[Django] tutorial #3-view

์ •๋ณด๊ตฌ๋‹ˆยท2021๋…„ 11์›” 26์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
5/15
post-thumbnail

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)



404 ์˜ˆ์™ธ ์ผ์œผํ‚ค๊ธฐ


ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ• ๋•Œ ์—๋Ÿฌ๋ฅผ ์–ผ๋งˆ๋‚˜ ์ž˜ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ€๋Š” ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋‹ค.

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>



๐Ÿ”Ž ์ฐธ๊ณ 
๋””์žฅ๊ณ  ๊ณต์‹๋ฌธ์„œ
๋””์žฅ๊ณ  ๊ณต์‹๋ฌธ์„œ ๊ฐ•์˜์ž๋ฃŒ

0๊ฐœ์˜ ๋Œ“๊ธ€