view
์ถ๊ฐํ๊ธฐ๋ทฐ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด polls/views.py
๋ฅผ ์ด์ด ๋ค์ ์ฝ๋๋ฅผ ์์ฑํด์ค๋ค.
- polls/views.py
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
์ฌ๊ธฐ์ ๋ช
์ฌํด์ผ ํ ๊ฒ์
๋ทฐ์์๋ ํด๋ผ์ด์ธํธ๋ก ๋ถํฐ request
๋ฅผ ๋ฐ๊ฒ ๋๊ณ , ๋ค์ response
๋ฅผ ๋ฐํ ํด์ค๋ค๋ ๊ฒ์ด๋ค.
์ด๋ request
์๋ ๋ง์ ์ ๋ณด๋ค์ด ๋ด๊ฒจ์๋ค.
response
๋ฅผ ํด์ฃผ๊ธฐ ์ ์ ๋ทฐ์์๋ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ ์๋ ์๊ณ ์ ์ฅํ ์ ๋ ์์ผ๋ฉฐ, ์น์ ๋ง๋ ๋ค์ํ ์ฒ๋ฆฌ๋ฅผ ํด์ค ์ ์๋ค.
์ถ๊ฐํด์ค ๋ทฐ๋ฅผ ํธ์ถํ๊ธฐ ์ํด polls/urls.py
๋ฅผ ์ด์ด url ์ฝ๋๋ฅผ ์์ฑํด์ค๋ค.
- polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
/polls/
์ฃผ์๋ก index
๋ทฐ๊ฐ ํธ์ถ์ด ๋๊ณ ,
ํธ์ถ ๋ index
๋ทฐ๊ฐ "Hello, world. You're at the polls index." ๋ผ๋ respose
๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํด์ฃผ๋ ๊ฒ์ด๋ค.
๋ค์์ ์ฅ๊ณ ์์ ์ง์ํ๋ url ํจํด์ด๋ค.
-<int:question_id>/
-<int:question_id>/results/
-<int:question_id>/vote/
์ฌ๊ธฐ์ ์ฃผ์ํด์ผํ ์ ์
url ์ฝ๋์ ๋ช
์๋ question_id
๋ ๋ทฐ์ ์๋ ํ๋ผ๋ฏธํฐ ์ค question_id
์ ์ผ์นํด์ผํ๋ค๋ ์ ์ด๋ค.
view
๊ฐ ์ค์ ๋ก ๋ญ๊ฐ๋ฅผ ํ๋๋ก ๋ง๋ค๊ธฐํ์ฌ๊น์ง๋ index
๋ทฐ๋ฅผ ํธ์ถํ์ ๋
"Hello, world. You're at the polls index."๋ผ๋ str์ ๋ฐํํ๋ค๋ฉด,
์ด์ ์์คํ ์ ์ ์ฅ๋ ์ต์ํ 5๊ฐ์ ํฌํ์ง๋ฌธ์ด ์ฝค๋ง๋ก ๋ถ๋ฆฌ๋์ด, ๋ฐํ์ผ์ ๋ฐ๋ผ ์ถ๋ ฅ๋๋๋ก ํด๋ณผ ๊ฒ์ด๋ค.
- polls/views.py
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
# Leave the rest of the views (detail, results, vote) unchanged
์ด์ index
๋ทฐ๊ฐ ํธ์ถ๋๋ฉด ํด๋ผ์ด์ธํธ๋ก ๋ถํฐ request
๋ฅผ ๋ฐ์,
Question
๋ฐ์ดํฐ ์ค ์ถํ์ผ์(pub-date)
๋ฅผ ์ ๋ ฌํ์ฌ 5๊ฐ
๊น์ง ๊ฐ์ ธ์ค๊ณ , ์ด๋ฅผ ์ฝค๋ง(,)
๋ก ์ฐ๊ฒฐํ์ฌ str์ผ๋ก ๋ฐํํด์ฃผ๊ฒ ๋๋ค.
templates
๋ถ๋ฆฌํ๊ธฐ์ง๊ธ๊น์ง๋ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ง๋ ํ์ด์ง๊ฐ ๋ทฐ ๋ด์ ์์๋ค. ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ์ฌ ๋ฐ๋ก ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐํํด์ฃผ๋ ๋ฐฉ์์ธ ๊ฒ์ด๋ค.
ํ์ง๋ง ์ด๋ฌํ ๋ฐฉ์์ ๋์์ธ ์์ ์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ง๋ ํ์ด์ง ์์ ์ด ๋งค์ฐ ๋ณต์กํด์ง๋ค.
๋์์ธ์ ์์ ํ๊ธฐ ์ํด ๋ทฐ ๋ด๋ถ๋ฅผ ์์ ํด์ผํ๊ธฐ ๋๋ฌธ์ด๋ค
๋ฐ๋ผ์ ๋ด๋ถ๋ก์ง ๋ด๋น์ธ ๋ทฐ์ ๋์์ธ ๋ด๋น์ธ ํ ํ๋ฆฟ์ ๋ถ๋ฆฌ์์ผ์ค์ผ ํ๋ค !
์ฐ์ polls
๋๋ ํ ๋ฆฌ์ templates
๋ผ๋ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ค๋ค.
์ด๋ templates
๋๋ ํ ๋ฆฌ ๋ด์ ์ฑ ์ด๋ฆ(polls)
์ผ๋ก ๋๋ ํ ๋ฆฌ๋ฅผ ๋ํ๋ ๋ง๋ค์ด html
ํ์ผ์ ๊ด๋ฆฌํด์ค์ผ ํ๋ค.
๊ทธ ์ด์ ๋ ์ฑ์ด๋ฆ์ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ํ๋ ๋ง๋ค์ด ์ฃผ์ง ์์ผ๋ฉด ์ฅ๊ณ ๊ฐ ๋ค๋ฅธ ์ฑ์ ํ ํ๋ฆฟ๊ณผ ๊ตฌ๋ถํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ด๋ค.
polls/
migrations/
templates/polls
index.html
ํ ํ๋ฆฟ์ ๋ค์ ์ฝ๋๋ฅผ ์์ฑํด์ค๋ค.
- polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
๋ง๋ค์ด์ค ํ
ํ๋ฆฟ์ ์ด์ฉํ์ฌ polls/views.py
์ index
๋ทฐ๋ฅผ ์
๋ฐ์ดํธ ํด์ค๋ค
- polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
์ฝ๋๋ฅผ ์ดํด๋ณด์๋ฉด,
teplate
์ load
ํด์ respose
๋ฅผ ๋ฐํํด์ฃผ๋ ๊ฒ์ด๋ค.
์ด๋ context
๋ฅผ ํตํด์ ํ
ํ๋ฆฟ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํด์ค๋ค.
latest_question_list
๋ฐ์ดํฐ๋ฅผ ํ
ํ๋ฆฟ์ ์ ๋ฌํด์ฃผ๋ฉด ํ
ํ๋ฆฟ์์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
ํ
ํ๋ฆฟ์ ๋ทฐ๋ก๋ถํฐ ๋ฐ์ดํฐ(latest_question_list)
๋ฅผ ๋ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ค๋ค !
render()
๋ก ์ฝ๋๋ ์ค์ด๊ธฐrender
ํจ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ฝ๋๋์ ์ค์ผ ์ ์๋ค.
์ฅ๊ณ ์์๋ ์ ํํ๋ ์์
์ ์์ค์ฝ๋๋ฅผ ์ค์ด๊ธฐ ์ํด ๊ฐ๋จํ ํจ์๋ก ํํํ ์ ์๋๋ก ๋จ์ถ ๊ธฐ๋ฅ(shortcuts)
๋ฅผ ์ ๊ณตํ๋ค.
- polls/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
ํ๋ก๊ทธ๋๋ฐ์ ํ ๋ ์๋ฌ๋ฅผ ์ผ๋ง๋ ์ ์ฒ๋ฆฌํ๋ ๊ฐ๋ ์ค์ํ ๋ถ๋ถ์ด๋ค.
detail
๋ทฐ์ ์๋ฌ๋ฅผ ๋ฐ์์์ผ๋ณด๋ ค๊ณ ํ๋ค.
- polls/views.py
from django.http import Http404
from django.shortcuts import render
from .models import Question
# ...
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})
- polls/templates/polls/detail.html
{{ question }}
์ฝ๋๋ฅผ ์์ฑํด์ค ๋ค ์๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ฉด ์๋ฌ๊ฐ ๋ํ๋๋ฉด์ ์์ธ ๋ฉ์์ง์ธ "Question does not exist"๋ฅผ ๋ฐํํด์ค๋ค
get_object_or_404()
๋ฐ์ดํฐ๊ฐ ์กด์ฌํ์ง ์์๋ get()
์ ์ฌ์ฉํ์ฌ Http404 ์์ธ๋ฅผ ๋ฐ์์ํค๋ ๊ฒ์ ์์ฃผ ์ฌ์ฉํ๋ค. ์ฅ๊ณ ๋ ์ด ๊ธฐ๋ฅ์ ๋ํ ๋จ์ถ ๊ธฐ๋ฅ(shortcuts)
์ ์ ๊ณตํ๋ค.
๋ฐฉ๊ธ ์ ์์ฑํ detail
๋ทฐ๋ฅผ ๋จ์ถ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์๋ค.
- polls/views.py
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)
return render(request, 'polls/detail.html', {'question': question})
detail
๋ทฐ๋ context
๋ณ์์ธ question
์ด ์ฃผ์ด์ก์ ๋, polls/detail.html
ํ
ํ๋ฆฟ์ด ์ด๋ป๊ฒ ๋ณด์ด๋์ง ํ์ธํด๋ณด๊ธฐ ์ํด
๋ค์๊ณผ ๊ฐ์ด ์์ ํด๋ณด๊ฒ ๋ค.
- 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>
์ด๋ ์ฝ๋ ํด์์ ํด๋ณด์๋ฉด, ๋ค์๊ณผ ๊ฐ๋ค
<h1>{{ question.question_text }}</h1>
: ํ
ํ๋ฆฟ์ question
์ ๋๊ฒจ๋ฐ๊ณ question
๋ฐ์ดํฐ ๋ด์ question_text
{% for choice in question.choice_set.all %}
: question
์ ์ธ๋ํค(ForeignKey)
๋ก ๋ฐ๋ choice
๋ค์ ๋ชจ๋ ๊ฐ์ ธ์จ๋ค<li>{{ choice.choice_text }}</li>
: ๊ฐ์ ธ์จ choice
๋ฅผ ๋ฆฌ์คํธ์๋ค์ฝ๋ ์์ ํ ์๋ฒ๋ฅผ ํ์ธํด๋ณด๋ฉด question_text
๊ฐ ์ ๋ชฉ์ด ๋๊ณ ,
๊ทธ ์๋ choice
์ ๋ชจ๋ ๋ชฉ๋ก๋ค์ด ๋ณด์ด๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
url
์ ๊ฑฐํ๊ธฐํ์ฌ index.html
ํ์ผ์ url
๋ถ๋ถ์ด ํ๋์ฝ๋ฉ ๋์ด์๋ค.
- polls/templates/polls/index.html
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
์ด๋ ๊ฒ ๋ ๊ฒฝ์ฐ url
๋ณ๊ฒฝ ์, ํ
ํ๋ฆฟ์ ์๋ url
๋ค๋ ์ผ์ผํ ๋ณ๊ฒฝํด์ค์ผ ๋๋ค.
์ด๋ฅผ ๊ฐ์ ํ๊ธฐ ์ํด์
์ฅ๊ณ ์์๋ url
๋ง๋ค name
์ ๋ช
์ํด์ค ์ ์๋ค.
url
์ฝ๋์ name
์ ๋ช
์ํด์ฃผ๊ณ , ํ
ํ๋ฆฟ์ ๊ทธ name
์ ์ง์ ์จ์ฃผ๊ฒ ๋๋ฉด
์์ ์ด ์์ด๋ url
์ ๊ณ ์ ๋ค์์ด ์๊ธฐ ๋๋ฌธ์ ํ
ํ๋ฆฟ ๋ด์ ์์ค์ฝ๋๋ฅผ ๋ณ๊ฒฝํด์ค ํ์๊ฐ ์๊ฒ ๋๋ ๊ฒ์ด๋ค !
๋ฐ๋ผ์ ํ๋์ฝ๋ฉ๋ url
์ ์ ๊ฑฐํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ ํด์ค๋ค.
- polls/templates/polls/index.html
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
์ด๋ ํด๋น url
์ ๊ณ ์ ๋ค์์ polls/ulrs.py
ํ์ผ์์ ํ์ธํ ์ ์๋ค.
- polls/ulrs.py
...
# the 'name' value as called by the {% url %} template tag
path('<int:question_id>/', views.detail, name='detail'),
...
url
์ ์ด๋ฆ๊ณต๊ฐ ์ ํ๊ธฐ์ง๊ธ๊น์ง๋ polls
๋ผ๋ ์ฑ ํ๋๋ง ์์์ง๋ง,
์ค์ ํ๋ก์ ํธ๋ฅผ ํ๋ฉด ์ฑ์ด ์ฌ๋ฌ๊ฐ๊ฐ ์์ ์ ์๋ค.
polls
์ฑ์์ detail
์ด๋ผ๋ ๋ทฐ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ์๋ฅผ ๋ค์ด ๋ ๋ค๋ฅธ blog
์ฑ์์๋ ๋์ผํ๊ฒdetail
์ด๋ผ๋ ๋ทฐ๋ฅผ ๊ฐ์ง ์ ์๋ค.
์ด๋ ๋ฐฉ๊ธ ์์์ ํ๋ ๊ฒ์ฒ๋ผ
ํ
ํ๋ฆฟ์์์ url
ํ๊ทธ๋ฅผ {% url %}
์ด๋ ๊ฒ ํด์ค๋ค๋ฉด, ์ฅ๊ณ ๋ ์ด๋ค ์ฑ์ ๋ทฐ์์ ์์ฑ๋ url
์ธ์ง ๊ตฌ๋ถํ ์ ์๋ค.
๋ฐ๋ผ์ ์ฅ๊ณ ๊ฐ ์ฑ๋ง๋ค์ url
์ ๊ตฌ๋ถํ ์ ์๋๋ก,
๋ค์๊ณผ ๊ฐ์ด ํด๋น ์ฑ์์ ์ฌ์ฉํ๋ url
์ ๋ํด์ ์ฑ ์ด๋ฆ์ ๋ช
์ํด์ค์ผ ํ๋ค.
- 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'),
]
์ด๋ฆ์ ๋ช ์ํด์ค๋ค ํ ํ๋ฆฟ์๋ ์ถ๊ฐํด์ค๋ค.
- polls/templates/polls/index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
๐ ์ฐธ๊ณ
๋์ฅ๊ณ ๊ณต์๋ฌธ์
๋์ฅ๊ณ ๊ณต์๋ฌธ์ ๊ฐ์์๋ฃ