본문 바로가기
Django

[Django] DB 가져와서 CRUD(3) - DB 읽고 화면에 출력하기(R)

by 기록하는_사람 2022. 12. 19.

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 읽고 화면에 출력하기 - 결과

댓글