DB 읽고 화면에 출력하기
📌 index.html에 표 띄우기
(books/views.py)
from django.shortcuts import render
from .models import Books
# Create your views here.
def index(request):
b = Books.objects.all()
context = {
"bset" : b
}
return render(request, "books/index.html", context)
(templates/books/index.html)
<h1>INDEX PAGE</h1>
<table border="1">
<tr>
<th>NO</th>
<th>TITLE</th>
<th>WRITER</th>
<th>HIT</th>
</tr>
{% for i in bset %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ i.name }}</td>
<td>{{ i.writer }}</td>
<td>{{ i.hit }}</td>
</tr>
{% empty %}
<tr>
<th colspan="4">NO DATA</th>
</tr>
{% endfor %}
</table>
DB 읽고 화면에 출력하기 - 결과
📌 DB 읽고 화면에 출력하기 - 결과
'Django' 카테고리의 다른 글
[Django] url 별칭 (0) | 2022.12.19 |
---|---|
[Django] <var> (0) | 2022.12.19 |
[Django] DB 가져와서 CRUD(2) - DB 생성 및 레코드 추가 (0) | 2022.12.19 |
[Django] DB 가져와서 CRUD(1) - 기본 세팅 (0) | 2022.12.19 |
[Django] DB 가져오기(+ Template Tag) (0) | 2022.12.12 |
댓글