template๊ณผ view
์์ Django View & URL (2) ์์๋ View์์ ํ์ด์ง์ ๋์์ธ์ ํ๋์ฝ๋ฉ ํ๊ณ ์๋ ๋ฌธ์ ์ ์ด ์์๋ค. ์ด์ template์ ์์ฑํ์ผ๋ฏ๋ก view๋ฅผ updateํด๋ณผ๊ฒ์ด๋ค.
Copy # polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
last_question_list = Question.objects.order_by('-pub_date')[:5]
# polls/index.html ํ
ํ๋ฆฟ ๊ฐ์ ธ์ค๊ธฐ
template = loader.get_template('polls/index.html')
# template์์ ์ฐ์ด๋ ๋ณ์๋ช
๊ณผ, python ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐํ๋ dictionary
context = {
'last_question_list': last_question_list,
}
# context ์ ๋ฌํ๊ธฐ
return HttpResponse(template.render(context,request))
render()
template์ context๋ฅผ ์ ๋ฌํด ํํํ ๊ฒฐ๊ณผ๋ฅผ HttpResponse๊ฐ์ฒด์ ํจ๊ป returnํด์ฃผ๋ ๊ตฌ๋ฌธ์ ์์ฃผ์ฐ๋ ์ฉ๋ฒ์ด๋ค. Django๋ ์ด๋ฅผ ์ฝ๊ฒ ํํํ ์ ์๊ฒ ๋จ์ถ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋ค.
Copy from django.shortcuts import render
from .models import Question
def index(request):
last_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'last_question_list': last_question_list,
}
return render(request, 'polls/index.html', context)
loader
์ HttpResponse
๋ฅผ ๋ ์ด์ importํ์ง ์์๋ ๋๋ค.
Copy render(request, template_name, context=None, content_type=None, status=None, using=None)
renderํจ์๋ request, template_name์ ์ฒซ๋ฒ์งธ, ๋๋ฒ์งธ ์ธ์๋ก ๋ฐ์ผ๋ฉฐ, ๋ค์๋ถํฐ๋ ์ ํ์ (optional)์ธ์๋ก ๋ฐ๋๋ค. renderํจ์๋ ์ธ์๋ก ์ง์ ๋ context๋ก ํํ๋ template์ HttpResponse ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ค.
404 Error
Copy from django.shortcuts import render
from django.http import Http404
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
http://127.0.0.1:8000/polls/1/
๋ก ์ ์ํด๋ณด๋ฉด ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
ํ
ํ๋ฆฟ์ด ์กด์ฌํ์ง ์๋๋ค๋ ์ค๋ฅ์ด๋ค.
polls/templates/polls
๋๋ ํ ๋ฆฌ ํ์์ detail.html
ํ
ํ๋ฆฟ์ ์์ฑํด์ฃผ๊ณ ๋ ํ ์๋ก๊ณ ์นจ์ ํ๋ฉด ์ค๋ฅ๊ฐ ์๋๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
get_object_or_404()
๊ฐ์ฒด๊ฐ ์กด์ฌํ์ง ์์ ๋ get()
์ ์ด์ฉํ์ฌ Http404์์ธ๋ฅผ ๋ฐ์์ํค๋ ๊ฒ์ ์์ฃผ ๋ฐ์ํ๋ค. ์์ render()
์ฒ๋ผ Django์์ ๋จ์ถ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋ค.
Copy from django.shortcuts import get_object_or_404, render
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
URL
template์์ ํ๋์ฝ๋ฉ๋ url์ `
` ์ ์ฌ์ฉํ์ฌ ์์กด์ฑ์ ์ ๊ฑฐํ ์ ์๋ค.
Copy # urls.py
# the 'name' value as called by the {% url %} template tag
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/',views.results, name='results'),
path('<int:question_id>/vote/',views.vote, name='vote'),
]
์ฌ๊ธฐ์ name์ template tag `
`์์ ํธ์ถํ ๋ ๋ถ๋ฅด๋ ์ด๋ฆ์ด๋ค.
Copy {# template #}
{% for question in last_question_list %}
<li><a href="/polls/{{question.id}}/">{{ question.question_text}}</a></li>
{% endfor %}
๋ค์๊ณผ ๊ฐ์ด ํ๋์ฝ๋ฉ๋ ๋ถ๋ถ์ ์๋์ ๊ฐ์ด ๋ณ๊ฒฝํ ์ ์๋ค.
Copy <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
ํ์ง๋ง project์ ์ฌ๋ฌ๊ฐ์ app์ด ์กด์ฌํ๊ณ , ๊ฐ์ path name์ ๊ฐ์ง๊ณ ์์ ์ ์๋ค. URLconf ์ namespace๋ฅผ ์ถ๊ฐ ํจ์ผ๋ก์จ ํด๊ฒฐํ ์ ์๋ค.
Copy # polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/',views.results, name='results'),
path('<int:question_id>/vote/',views.vote, name='vote'),
]
Copy {# polls/index.html #}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text}}</a></li>
์ฐธ์กฐ