10 Sun
TIL
[๋ฐฐํ์ ์ค์ง๋ ํ์ด์ฌ ์นํ๋ก๊ทธ๋๋ฐ]
3 ํํ ๋ฆฌ์ผ ๋ฐ๋ผํ๊ธฐ - ์ค๋ฌธ์กฐ์ฌ
3.10 404 ์ค๋ฅ ์ผ์ผํค๊ธฐ
404์ค๋ฅ๋ ์น ์๋น์ค์์ ํ์ผ์ด ์กด์ฌํ์ง ์์ ๋ ๋ฐ์ํ๋ ์ค๋ฅ์ด๋ค. ๊ฒ์ํ์์ ์ ๋ณด๋ฅผ ๋ถ๋ฌ ์ฌ ๋ ํ์ด์ง์ ํด๋น ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ์ง ์๋ ์๋ฏธ๋ก ์ฌ์ฉํ๋ค.
polls/views.py
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})
Http404๋ฅผ ์ด์ฉํ๋ฉด ํฌํ ํญ๋ชฉ์ด ์์ ๊ฒฝ์ฐ 404 ์ค๋ฅ๋ฅผ ๋ฐ์์ํจ๋ค. ์ด์ ์ index๋ทฐ์ ๊ฐ์ด detail ๋ทฐ์์ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋ฏ๋ก detail.html์ ์์ฑํ๋ค.
Http404๋ฅผ ์ฒ๋ฆฌํ ๋๋ loade-rrender ๊ด๊ณ์ฒ๋ผ ๋จ์ถ ํจ์๊ฐ ์กด์ฌํ๋๋ฐ get_object_or_404
์ด๋ค. ์ด ํจ์๋ฅผ ์ฌ์ฉํด detail ๋ทฐ๋ฅผ ์์ ํ๋ค.
polls/views.py
from django.shortcuts import get_object_or_404
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
render์์ get_object or 404๋ฅผ ์ถ๊ฐํด ์ํฌํธ ํ๋ค. ๊ทธ๋ฆฌ๊ณ detail ๋ทฐ์์๋ try except ๊ตฌ๋ฌธ์ ์์ ๊ณ 404๋ฅผ ์ด์ฉํด ์ฝ๋๋ฅผ ๊ฐ์ํ ํ๋ค. ์ด๋ฅผ detail.html ์ ์ถ๊ฐํ๋ค.
polls/templates/polls/detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li> {{ choice.choice_text }} </li>
{% endfor %}
</ul>
</body>
</html>
3.11 ํ๋ ์ฝ๋ฉ๋ URL ์์ ๊ธฐ
index.html
ํ์ผ์ ๋ณด๋ฉด ๋งํฌ์ ์ฃผ์๊ฐ ํ๋์ฝ๋ฉ ๋์ด์๋ค. ์ด๋ ๋ค๋ฅธ ํํ๋ก ๋ณ๊ฒฝ์ ์ผ์ผ์ด html์ ๋ค ์ด์ด์ผ ํ๋ค๋ ๋ถํธํจ์ด ์๋ค. ๋ฐ๋ผ์ URL ํ
ํ๋ฆฟ ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ ์ฝ๋ฉ๋ URL์ ์์ค๋ค.
{% for question in latest_question_list %}
<li><a href={% url 'detail' question.id %}>{{ question.question_text }}</a></li>
{% endfor %}
URL ํ ํ๋ฆฟ ํ๊ทธ๋ฅผ ์ฌ์ฉํด ์ฃผ์๋ฅผ ๋ง๋ค์ด ์ถ๋ ฅํ๋ค. URL ํ ํ๋ฆฟ ํ๊ทธ๋ URL์ ์ด๋ฆ์ ํ์ ์ธ์๋ก ์ ๋ฌ ๋ฐ๋๋ค. ํด๋น ์ด๋ฆ์ ๊ฐ์ง URL์ ulrs.py ์ ์ฒด๋ฅผ ๊ฒ์ํด ์ฐพ๋๋ค.
3.12 URL ๋ค์ ์คํ์ด์ค ์ค์ ํ๊ธฐ
detail์ด๋ผ๋ ๋ทฐ๊ฐ polls์๋ ์๊ณ ๋ค๋ฅธ ์ฑ์๋ ์์ ๊ฒฝ์ฐ ์ฅ๊ณ ๋ ์ด๋ ๋ทฐ์ URL์ ๋ง๋ค์ง ์ ์๊ฐ ์๋ค. ์ด๋ฐ ๊ฒฝ์ฐ ๋ค์์คํ์ด์ค๋ฅผ ์ค์ ํด ๊ฐ๊ฐ์ ๋ทฐ๊ฐ ์ด๋ ์ฑ์ ์ํ ๊ฒ์ธ์ง ๊ตฌ๋ถํ ์ ์๋๋ก ํ๋ค. ํ๋ก์ ํธ๊ฐ ๋ณต์กํด์ง์๋ก ์๋ ๊ฒ์ด ํธ๋ฆฌํ๋ค.
polls/urls.py
app_nmae = 'polls'
polls/template/polls/index.html
{% for question in latest_question_list %}
<li><a href={% url 'polls:detail' question.id %}>{{ question.question_text }}</a></li>
{% endfor %}
3.13 ๊ฐ๋จํ ํผ ์ ์
ํํ ๊ธฐ๋ฅ์ด ๋์ํ๋ ค๋ฉด detail.html์ ์์ ํ๊ณ vote ๋ทฐ์๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด์ผ ํ๋ค.
detail.html
<body>
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% 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 %}
<input type="submit" value="Vote">
</form>
</ul>
</body>
form ํ๊ทธ๋ฅผ ์ฌ์ฉํด์ ์ฌ์ฉ์๊ฐ ๋ต๋ณ ํญ๋ชฉ์ ์ ํํ๊ณ ์ ๋ฌํ ์ ์๋ค.
์ฌ์ฉ์๊ฐ ์ ํํ ํญ๋ชฉ์ ๋ฒํธ๋ฅผ vote ๋ทฐ๋ฅผ ์ ๋ฌํ๋๋ก action ์์ฑ์ vote URL์ด ์ถ๋ ฅ๋๊ฒ URL ํ ํ๋ฆฟ ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ค.
method = post๋ HTTP ๋ฉ์๋ ์ค ํ๋์ด๋ฉฐ ์๋ฒ๋ก ์ ๋ณด๋ฅผ ์ ๋ฌํ ๋ ์ฌ์ฉํ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ
forloop.counter๋ ํ ํ๋ฆฟ ๋ฌธ๋ฒ์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ผ๋ก ๋ฐ๋ณต๋ฌธ์ ๋ฐ๋ณต ํ์๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ๊ธฐ๋ฅ. ์ฌ๊ธฐ์๋ vote๋ทฐ์ choice=๋ฒํธ ํํ๋ก ์ ๋ฌํ๋ค
csrftoken์ CSRF ๊ณต๊ฒฉ์ ๋ง๊ธฐ์ํ ์๋จ์ด๋ค. ๋ฐฉ๊ธ ์๋ฒ๋ก ๋ค์ด์จ ์์ฒญ์ด ์ฌ์ดํธ ๋ด๋ถ์์ ์จ ๊ฒ์ด ๋ง๋์ง ํ์ธํ๋ ์ฉ๋๋ก csrftoken์ ๊ฐ์ ์ฌ์ฉ
polls/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .models import Question, Choice
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
request.POST[๋ณ์์ด๋ฆ]์ ํตํด ์ ๋ฌ๋ฐ์ ๋ณ์์ ๊ฐ์ ํ์ธํ๋ค. ์ด ๋ ์ ๋ฌ๋๋ ๊ฐ์ ํญ์ ๋ฌธ์์ด์ด๋ค.
์ ๋ฌ๋ฐ์ ๋ต๋ณ์ด ํด๋น ํฌํ ํญ๋ชฉ์ ์๋์ง ํ์ธํ๊ณ ์์ผ๋ฉด ๋ค์ ์์ธ ํ์ด์ง๋ก ์ด๋ํ๋ค. ์ด ๋ ๋ต๋ณ์ ์ ํํ์ง ์์๋ค๋ ์ค๋ฅ ๋ฉ์์ง๋ ๊ฐ์ด ์ ๋ฌ๋๋ค.
๋ฐ๋๋ก ์ ๋๋ก ๋ ๋ต๋ณ์ด ์ ํ๋๋ฉด ํด๋น ๋ต๋ณ ์๋ฅผ 1 ์ฆ๊ฐ์ํค๊ณ ๊ฒฐ๊ณผ ํ๋ฉด์ผ๋ก ์ด๋ํ๋ค.
polls/views.py
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ result๋ทฐ
polls/templates/polls/results.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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>
</body>
</html>
results.html์ ๊ฐ ๋ต๋ณ ํญ๋ชฉ๊ณผ ํฌํ ์๋ฅผ ํ๊บผ๋ฒ์ ๋ณด์ฌ์ค๋ค.
3.14 ์ ๋ค๋ฆญ ๋ทฐ ์ฌ์ฉ
์ ๋ค๋ฆญ๋ทฐ๋ ์ฅ๊ณ ์์ ๋ฏธ๋ฆฌ ์ค๋นํ ๋ทฐ๋ฅผ ์๋ฏธ. ์น ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ทฐ๋ค์ ์ด๋ฏธ ์ฅ๊ณ ์์ ๋๋ถ๋ถ ๋ง๋ค์ด์ ธ ์๋ค.
Last updated
Was this helpful?