Django Template
-
setting.py
:APP_DIRS
:True
๋ก ์ค์ ํ๊ธฐ. -
๋ง๋ App ์์
App์ด๋ฆ/templates/app์ด๋ฆ
์ ํด๋ ์์ฑApp์ด๋ฆ/emplate/App์ด๋ฆ ๋ผ๊ณ ๋ง๋ค ํ์ ์์ด, ๊ทธ๋ฅ App์ด๋ฆ/templates์ ๋ฃ์ด๋ ๋์ง ์์๊น? ๋ง์ฝ ๋์ผํ template ์ด๋ฆ์ด ๋ค๋ฅธ ์ดํ๋ฆฌ์ผ์ด์ ์ ์์ ๊ฒฝ์ฐ Django ๋ ์ด ๋๊ฐ์ ์ฐจ์ด๋ฅผ ๊ตฌ๋ถํ์ง ๋ชปํฉ๋๋ค.
404 Error
๊ธฐ์กด ๋ฐฉ์.
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})
์กฐ๊ธ ๋ ํธํ ๋ฐฉ๋ฒ.
from django.shortcuts import get_object_or_404, render
from .models import Question
# ...
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
# get_list_or_404() : list Check
return render(request, 'polls/detail.html', {'question': question})
Template ์์ ํ๋์ฝ๋ฉ๋ URL ์ ์ ๊ฑฐํ๊ธฐ
URL์ด ๋ณ๊ฒฝ๋๋ฉด URL์ ํฌํจํ ์ฝ๋๋ฅผ ์ ๋ถ ๋ณ๊ฒฝํด์ผํ๋ ๋ฌธ์ ์ ์ ํด๊ฒฐํ๊ธฐ ์ํด ์ ๊ฑฐํ๋ค.
๊ฐ๊ฒฐํฉ ์ฝ๋
html
<li><a href="/memos/{{ memo.id }}/">{{ memo.content }}</a></li>
์ฝ๊ฒฐํฉ ์ฝ๋
(r'^memos/(?P<memo_id>[0-9]+)/$', views.detail, name='detail')
html
URL ๊ตฌ์ญ๋๋๊ธฐ
๋ค๋ฅธ App์ URLS.py๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด
app_name = 'memos' # App ์ด๋ฆ ์ ์ธ