본문 바로가기
Django

[Django] 사진 CRUD(3) - DB 읽고 화면에 출력하기(R)

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

DB 읽고 화면에 출력하기

📌 index.html에 출력하기

① 데이터 가져오기.

(gal/views.py)

from django.shortcuts import render
from .models import Pic

# Create your views here.

def index(request):
    p = Pic.objects.all()
    context = {
        "pset" : p
    }
    return render(request, "gal/index.html", context)

 

② 출력.

(index.html)

<h1>INDEX PAGE</h1>

{% for i in pset %}
    <img src="{{ i.pic.url }}" height="150px">
{% endfor %}

<style>
    img:hover {
        opacity: 0.4;
    }
</style>

 

DB 읽고 화면에 출력하기 - 결과

📌 DB 읽고 화면에 출력하기 - 결과

댓글