layout.html, List.html 을 활용한 css 적용법
table 작성
models.py
from django.db import models
# Create your models here.
# Database Table을 class로 선언
class Article(models.Model):
code = models.CharField(max_length = 10)
name = models.CharField(max_length = 20)
price = models.IntegerField()
pub_date = models.DateTimeField()
table을 생성할 경우는 Migrations를 해야한다.
Migrations 후에 늘 하던데로 migrate도 해야한다. (사진 첨부는 하지 않았지만 방식은 비슷하다.)
생성된 파일
migrations 폴더 안에 생성된 파일을 확인할 수 있다. (위의 작업을 해주면 자동 생성된다.)
# Generated by Django 4.1.2 on 2022-10-17 07:06
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Article",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("code", models.CharField(max_length=10)),
("name", models.CharField(max_length=20)),
("price", models.IntegerField()),
("pub_date", models.DateTimeField()),
],
),
]
admin.py
관리자 페이지에서 사용할 정보를 기입한다.
from django.contrib import admin
from myapp.models import Article
# Register your models here.
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id','code','name','price','pub_date')
admin.site.register(Article, ArticleAdmin)
properties for을 참고하여 경로확인
Anaconda Prompt 에서 경로이동
cd C:\work\psou\django5_db
관리 계정 만들기
python manage.py createsuperuser
아이디 이메일 비번 (아래 예시는 임의로 생성)
korea_go
ab@abc.com
korea123
chrome url 창에 해당 주소를 입력
127.0.0.1/admin/
위에서 회원가입한 아이디로 로그인
urls.py
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.main),
path("show", views.show),
]
views.py
from django.shortcuts import render
from myapp.models import Article
# Create your views here.
def main(request):
return render(request, 'main.html')
def show(request):
# sql문, django orm 두가지 방식이 있는데 type이 다르다.
# sql = "select * from Article"
datas = Article.objects.all() # Django ORM
print(datas) # <QuerySet [<Article: Article object (1)>, <Article: Article object (2)>]>
print(datas[0].name)
return render(request, 'List.html', {'articles':datas})
main.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
메인<p/>
메뉴1 메뉴2 <a href="show">자료보기</a>
</body>
</html>
layout.html
<h2>** 자료보기 **</h2>
<div style="color:blue;">
{% block content %}
{% endblock %}
</div>
List.html
layout.html을 아래의 코드로 가져와서 작성했다. 추후에 일부 모듈화 하기에
{% extends 'layout.html' %}
{% block content %}
{% endblock %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
{% extends 'layout.html' %}
{% block content %}
Article 데이터 출력 <p/>
{% if articles.count > 0 %}
{% for a in articles %}
<b>{{a.code}}</b> {{a.name}} {{a.price}} {{a.pub_date}}<br>
{% endfor %}
{% else %}
<p>자료가 없어요</p>
{% endif %}
{% endblock %}
</body>
</html>
'Django' 카테고리의 다른 글
django7 - DB, multi-table (제조사, 상품 관리) (0) | 2022.10.18 |
---|---|
django6 - Maria DB (방명록) (0) | 2022.10.17 |
django4 - session 예제(간이 쇼핑몰, 장바구니 기능) (0) | 2022.10.17 |
django3 - session (0) | 2022.10.17 |
django2 - GET, POST, django 기본 구조 (0) | 2022.10.17 |