데브코스 TIL/Django, API

Django Tutorial Part 3

예니ㅣ 2023. 11. 6. 13:14

16. 뷰(Views)와 템플릿(Templates)

polls/templates/polls/index.html

<ul>
    <li>{{first question}}</li>
<ul>

 

polls/views.py

from .models import *
from django.shortcuts import render

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'first_question': latest_question_list[0]}
    return render(request, 'polls/index.html', context)
>>> from polls.models import *
>>> Question.objects.order_by('-pub_date')[:5] 
>>> print(Question.objects.order_by('-pub_date')[:5].query) 
SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date" FROM "polls_question" ORDER BY "polls_question"."pub_date" DESC LIMIT 5

 

 

17. 템플릿(Templates)에서 제어문 사용하기

polls/templates/polls/index.html

{% if questions %}
<ul>
    {% for question in questions %}
        <li>{{question}}</li>
    {% endfor %}
</ul>
{% else %}
<p>no questions</p>
{% endif %}

 

polls/views.py

from .models import *
from django.shortcuts import render

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'questions': latest_question_list}
    #context = {'questions': []}
    return render(request, 'polls/index.html', context)

 

 

18. 상세(detail) 페이지 만들기

polls/views.py

def detail(request, question_id):
    question = Question.objects.get(pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

 

polls/urls.py

from django.urls import path
from . import views     # 현재 앱의 views 가져오기

urlpatterns = [
    path("", views.index, name="index"),
    path('some_url',views.some_url),
    path('<int:question_id>/', views.detail, name='detail'),
]

 

polls/templates/polls/detail.html

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

 

 

19. 상세(detail) 페이지로 링크 추가하기

polls/urls.py

from django.urls import path 
from . import views  

app_name = 'polls'

urlpatterns = [     
    path('', views.index, name='index'),
    path('some_url', views.some_url), 
    path('<int:question_id>/', views.detail, name='detail'),     
    
]

 

polls/templates/polls/index.html

{% if questions %}
<ul>
    {% for question in questions %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
<ul>
{% else %}
<p>no questions</p>
{% endif %}

 

 

20. 404 에러 처리하기

"404 에러"는 자신에게 버그가 존재한다는 사실을 알리는 것입니다.

 

polls/views.py

from models.py import *
from django.http import HttpResponse
from django.http import Http404
from django.shortcuts import render , get_object_or_404

...
def detail(request, question_id):
    """
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    """
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

 

 

 

 

 

 

 

 

 

 

 

'데브코스 TIL > Django, API' 카테고리의 다른 글

Django REST Framework Part 2  (0) 2023.11.06
Django REST Framework Part 1  (0) 2023.11.06
Django Tutorial Part 4  (0) 2023.11.06
Django Tutorial Part 2  (0) 2023.11.06
Django Tutorial Part 1  (0) 2023.11.06