#polls/views.py
#์ฒ์์
#ํด๋ผ์ด์ธํธ๋ก๋ถํฐ request๋ฅผ ๋ฐ์ผ๋ฉด HttpResponse๋ก ๋ฆฌํด!
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id) #response๋ก ํด๋ผ์ด์ธํธ์๊ฒ ์ ๋ฌ
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)
#polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/ , views.index(ํจ์)๋ฅผ ํธ์ถ
path('', views.index, name='index'),
# ex: /polls/5/ ,views.detail์ ํธ์ถ
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'),
]
# <int:question_id>/vote/ ๋ ์ฅ๊ณ ์์ ์ง์ํ๋ urlํจํด
# name์ ํ
ํ๋ฆฟ์ ์ urlํจํด ๋์ ์ด๋ฆ์ ์ฌ์ฉ
# question_id๋ view์ ํ๋ผ๋ฏธํฐ์ question_id์ ์ผ์น๋์ด์ผํจ.
#polls/views.py
from django.http import HttpResponse
from .models import Question
# question ๋ฐ์ดํฐ ์ค์์ ์ถํ์ผ์๋ฅผ ์ ๋ ฌํ์ฌ 5๊น์ง๋ง ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ฝค๋ง๋ก ์ฐ๊ฒฐํด ์คํธ๋ง์ผ๋ก ๋ง๋ค๊ณ
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) #๊ทธ ๋ฌธ์์ด์ response
โก๏ธ ์์ ์ฝ๋๋ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ฃผ๋ ์ฝ๋๊ฐ view๋ด์ ์กด์ฌ. ํ์ง๋ง ๋์์ธ ์์ ์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ฃผ๋ ํ์ด์ง ์์ ์ด ์ด๋ ค์ ๋ฐ๋ผ์ ๋ด๋ถ ์กฐ์ง ๋ด๋น๊ณผ ๋์์ธ ๋ด๋น์ธ templates๋ฅผ ๋ถ๋ฆฌํด์ผํจ.
<!-- 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
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๋ฅผ ๋ก๋ํด์ response
template = loader.get_template('polls/index.html')
#context๋ฅผ ํตํด latest_question_list์ ๋ฐ์ดํฐ๋ฅผ template์๊ฒ ์ ๋ฌํด template๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉ -> ์์ html์ฝ๋๋ฅผ ๋ณด๋ฉด latest_question_list ํ์ธ ๊ฐ๋ฅ
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
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}
# render() ํจ์์ {~:~}๋ก html ํ์ผ์๊ฒ ๋๊ฒจ์ฃผ๋ dict๋ฅผ context๋ผ๊ณ ๋ถ๋ฅธ๋ค.
return render(request, 'polls/index.html', context)
#polls/views.y
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})
get_object_or_404 ์ฌ์ฉํด ๋ ์ฝ๊ฒ ๋ง๋ค๊ธฐ
#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)
#list๊ฐ return ๋ ์์๋ get_object_or_404 ๋์ get_list_or_404๋ฅผ ํ์ฉํ๊ธฐ
return render(request, 'polls/detail.html', {'question': question})
<!-- polls/templates/polls/detail.html-->
<!-- template์ quesiton์ ๋๊ฒจ ๋ฐ๊ณ , question์ question_text๋ฅผ ์ ๋ชฉ์ผ๋ก ๋ณด์ฌ์ค -->
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
<!-- question.choice_set์ choice๋ค์ด question์ ์ธ๋ ํค๋ก ๊ฐ๊ณ ์๋ ๋ชจ๋๋ฅผ ๊ฐ์ง๊ณ ์๋ผ. ์๋ฅผ ๋ค์ด what's new๋ผ๋ ํ์ค์ฒ์ด ์์ผ๋ฉด what's new์ ๋ํด ์ธ๋ํค๋ฅผ ๊ฐ๋ ์ด์ด์ค๋ฅผ ๋ค ๊ฐ๊ณ ์๋ผ ๊ทธ๋ฆฌ๊ณ ๋ฆฌ์คํธ๋ก ๋ง๋ค์ด์ฃผ๊ธฐ-->
<!-- polls/index.html -->
<!-- ํ๋์ฝ๋ฉ -->
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
<!-- polls/index.html -->
<!-- ํน์ ๊ฒฝ๋ก๋ค์ ์์กด์ฑ์ ์ ๊ฑฐ -->
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
#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>
<!-- <a href="/polls/{{ question_id }}/">{{question.question_text}} </a> ํํ๋ฅผ ๊ฐ์ง -->
<!-- ์ด๋ถ๋ถ์ element์์ ํ์ธ ๊ฐ๋ฅ -->
: ํด๋ผ์ด์ธํธ์ธ ์ฌ์ฉ์๋ก๋ถํฐ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
์ฌ์ฉ์๊ฐ ํ์๊ฐ์
์ด๋ ์ฃผ๋ฌธ์ ํ๋ฉด ์๋ฒ์ชฝ์ผ๋ก ์์ฒญํ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์์ผํจ.
class ๊ธฐ๋ฐ ํ๋ก ๊ตฌํํ๋ฉด ์์ค์ฝ๋๊ฐ ์ค์ด๋ค์.
<!-- polls/templates/polls/detail.html -->
<!-- form ์์ ์ถ๊ฐ -->
<!-- question.choice_set.all๋ question์ ์ธ๋ํค๋ก ๋ฐ๋ ๋ชจ๋ ๊ฒ๋ค ๊ฐ์ ธ์ ํ๋์ฉ ๋์๋ผ -->
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
<fieldset>
<legend><h1>{{ question.question_text }}</h1></legend>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
</fieldset>
<input type="submit" value="Vote">
</form>
<!-- submit์ ๋๋ฅด๋ฉด ํด๋น url(= 'polls:vote ) ๋ก ๋ฐ์ดํฐ(value๊ฐ)๊ฐ ์ ๋ฌ(ํธ์ถ),๊ทธ๋ฆฌ๊ณ ํด๋น url์ ๊ฑธ๋ ค์๋ views.vote๊ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ -->
<!-- {% csrf_token %}์ ํดํน๋ฐฉ์ง๋ฅผ ์ํด ์ฝ์
-->
#polls/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Choice, Question
# ...
# ํฌํ๋ฅผ ๋ง์ณค์ ๋ ํด๋น post์์ฒญ์ ์ฒ๋ฆฌํด์ค vote ํจ์๋ฅผ ์์ ํด์ผํจ.
def vote(request, question_id): #question_id๋ฅผ ๋๊ฒจ๋ฐ์
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice']) #question์ ๋ํด ์ธ๋ํค๋ฅผ ๊ฐ๋ ์ ํ์ง(choice)๋ฅผ ๊ฐ์ ธ์ด(์กฐ๊ฑด์ด ์ ํ์ง์ค์์ pk๊ฐ์ด ํ
ํ๋ฆฟ์์ ๋๊ฒจ๋ฐ์ ๊ฐ)
# ํ
ํ๋ฆฟ์์ ํฌ์คํธ ๋ฐฉ์์ผ๋ก ํธ์ถ์ ํ๊ธฐ ๋ผ๋ฌธ์ name์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ผ(ํ
ํ๋ฆฟ๋ณด๋ฉด name์ด choice์ธ๊ฑฐ ํ์ธ ๊ฐ๋ฅ)
#detail.html์ <input type="radio" name="choice" value="{{ choice.id}}"์์ ๋ ๋ผ์จ ๊ฐ
# form์ผ๋ก ์ ์ถ๋ post request ์ ์ฒด์์ 'choice'๊ฐ namedls htmlํ๊ทธ์ value๋ฅผ ๊บผ๋ด๋ ์ฝ๋
#request.POST dict๋ด์ {'choice':7}๊ณผ ๊ฐ์ด ๋์
๋๋ฆฌ ํํ๋ก ์กด์ฌํจ.
except (KeyError, Choice.DoesNotExist): #์ ํ์ง๊ฐ ์์ผ๋ฉด ์์ธ ๋ฐ์
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else: # try๋ฌธ์์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋ ๊ฒฝ์ฐ ๋ง์ง๋ง์ ์คํ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ค๋ ์ ํ์ง์์ +1 ํ๊ณ ์ ์ฅ
selected_choice.votes += 1
selected_choice.save() # ์ด์ DB ์ ์ฅ
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
#post๋ก view๊ฐ ํธ์ถ๋ ๊ฒฝ์ฐ์๋ httpresonpsededirect๋ฅผ ํด์ค๋ค.
#reverse๋ ํ๋์ฝ๋ฉ์ ํ์ง ์๊ธฐ์ํด์ ์ฑ๋ค์,url๋ค์ ์ฌ์ฉํจ.
# ์ด results url๋ก ๋ค์ views.py์ result ํจ์๋ฅผ ํธ์ถ
# polls/views.py
#์์์ ์ด์ด ํจ์๊ฐ ํธ์ถํ๊ณ question์ ์กฐํํ ํ polls/results.htmlํ์ด์ง๊ฐ ๊ฒฐ๊ณผ ํ์ด์ง๋ก ๋ณด์ฌ์ค
from django.shortcuts import get_object_or_404, render
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
#์ด๋ question ๋ฐ์ดํฐ๊ฐ ๊ฐ์ด ๋์ด๊ฐ๊ณ ,
<!-- polls/templates/polls/results.html -->
<!-- ๋์ด๊ฐ ๋ฐ์ดํฐ๋ question.choice_set.all ์ฆ, questino์ ๋ํ ์ ํ์ง ๊ฐ๋ค์ด ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ list๋ก ๋ฟ๋ ค์ค. -->
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
-โก๏ธ ์ฌ๊ธฐ๊น์ง๊ฐ ์ฌ์ฉ์๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๋ฐฉ๋ฒ
#polls/urls.py
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'), #์ฅ๊ณ ๊ฐ ๋ฏธ๋ฆฌ ๋ง๋ as_view๋ฅผ ํธ์ถํด์ IndexView๋ฅผ ํธ์ถํจ.
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
#polls/views.py
#์ ๋๋ฆญ ๋ทฐ ์ฌ์ฉํด์ ์ฝ๋๊ฐ ์ค์ด๋ฌ
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
#๋ง์ฝ context์ ๋๊ฒจ์ฃผ๋ ์ด๋ฆ์ด ๋ชจ๋ธ ์ด๋ฆ๊ณผ ๋ค๋ฅด๋ค๋ฉด context_object_name๋ฅผ ๋ค์ ์ ํด์ฃผ๊ณ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ get_querysetํจ์๋ฅผ ํตํด ๋ค์ ์์ฑ
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question # ํ
ํ๋ฆฟ์์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ชจ๋ธ
template_name = 'polls/detail.html' # ์ฌ์ฉํ ํ
ํ๋ฆฟ ์ด๋ฆ
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
... # same as above, no changes needed.