🍋 ⚾️ 💻 🎬 🎮

Tech/Django

[Django] 04. Template에서 Model 확인하기

aeightchill 2025. 2. 12. 22:36
728x90

 

 

 


 

📌  간단한 커피 메뉴 생성하기

 

 

  📂  views.py

def coffee_view(request):

    return render(request, '', {})

 

  { }에 model에서 가져온 어떤 객체 즉 어떤 행들을 다 넣어주도록 할 것이다.

from .models import Coffee

def coffee_view(request):
	coffee_all = Coffee.objects.all() # .get(), .filter() ...
	return render(request, 'coffee.html', {"coffee_list" : coffee_all})

 

 

 

  📂  template  >  coffee.html

<!DOCTYPE html>
<html>
    <head>
        <title>Coffee List</title>
    </head>

    <body>
        <h1> My Coffee List </h1>
        <p>{{ coffee_list }}</p>
    </body>
</html>

 

 

 

  📂  urls.py

from homepage.views import coffee_view

path('coffee/',coffee_view)

 

 

 

127.0.0.1.:8000/coffee/ 접속하면

 

 

 

 


위의 QuerySet을 name, price로 나타낼 수 있게 다시 수정해보기

 

 

  📂  template  >  coffee.html

<!DOCTYPE html>
<html>
    <head>
        <title>Coffee List</title>
    </head>

    <body>
        <h1> My Coffee List </h1>
        {% for coffee in coffee_list %}
            <p>{{ coffee.name }} , {{ coffee.price }}</p>
        {% endfor %}
    </body>
</html>

 

 

 

127.0.0.1.:8000/coffee/ 에 다시 접속하면

 

coffee name, coffee price가 보여진다.

 

728x90

'Tech > Django' 카테고리의 다른 글

[Django] 05. Form으로 Template에서 Model 수정하기  (0) 2025.02.12
[Django] 03. Model  (0) 2025.02.12
[Django] 02. Template  (0) 2025.02.11
[Django] 01. View  (0) 2025.02.11
[Django] 00. Django 알아보기  (0) 2025.02.11