Django Template & View & URL

template๊ณผ view

์•ž์„  Django View & URL (2)์—์„œ๋Š” View์—์„œ ํŽ˜์ด์ง€์˜ ๋””์ž์ธ์„ ํ•˜๋“œ์ฝ”๋”ฉ ํ•˜๊ณ  ์žˆ๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๋‹ค. ์ด์ œ template์„ ์ƒ์„ฑํ–ˆ์œผ๋ฏ€๋กœ view๋ฅผ updateํ•ด๋ณผ๊ฒƒ์ด๋‹ค.

# polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question

def index(request):
    last_question_list = Question.objects.order_by('-pub_date')[:5]
    # polls/index.html ํ…œํ”Œ๋ฆฟ ๊ฐ€์ ธ์˜ค๊ธฐ
  template = loader.get_template('polls/index.html')
    # template์—์„œ ์“ฐ์ด๋Š” ๋ณ€์ˆ˜๋ช…๊ณผ, python ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” dictionary
  context = {
        'last_question_list': last_question_list,
    }
  # context ์ „๋‹ฌํ•˜๊ธฐ
    return HttpResponse(template.render(context,request))

render()

template์— context๋ฅผ ์ „๋‹ฌํ•ด ํ‘œํ˜„ํ•œ ๊ฒฐ๊ณผ๋ฅผ HttpResponse๊ฐ์ฒด์™€ ํ•จ๊ป˜ returnํ•ด์ฃผ๋Š” ๊ตฌ๋ฌธ์€ ์ž์ฃผ์“ฐ๋Š” ์šฉ๋ฒ•์ด๋‹ค. Django๋Š” ์ด๋ฅผ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋‹จ์ถ•๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

from django.shortcuts import render
from .models import Question

def index(request):
    last_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {
        'last_question_list': last_question_list,
    }
    return render(request, 'polls/index.html', context)

loader์™€ HttpResponse ๋ฅผ ๋” ์ด์ƒ importํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

render(request, template_name, context=None, content_type=None, status=None, using=None)

renderํ•จ์ˆ˜๋Š” request, template_name์„ ์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋ฐ›์œผ๋ฉฐ, ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์„ ํƒ์ (optional)์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. renderํ•จ์ˆ˜๋Š” ์ธ์ˆ˜๋กœ ์ง€์ •๋œ context๋กœ ํ‘œํ˜„๋œ template์˜ HttpResponse๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

404 Error

from django.shortcuts import render
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})

http://127.0.0.1:8000/polls/1/ ๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜ค๋ฅ˜์ด๋‹ค.

polls/templates/polls ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์— detail.html ํ…œํ”Œ๋ฆฟ์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ๋‚œ ํ›„ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ์•ˆ๋‚˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

get_object_or_404()

๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ get()์„ ์ด์šฉํ•˜์—ฌ Http404์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์€ ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค. ์œ„์˜ render() ์ฒ˜๋Ÿผ Django์—์„œ ๋‹จ์ถ•๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

from django.shortcuts import get_object_or_404, render

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

URL

template์—์„œ ํ•˜๋“œ์ฝ”๋”ฉ๋œ url์„ `

` ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

# urls.py
# the 'name' value as called by the {% url %} template tag
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'),
]

์—ฌ๊ธฐ์„œ name์„ template tag `

`์—์„œ ํ˜ธ์ถœํ•  ๋•Œ ๋ถ€๋ฅด๋Š” ์ด๋ฆ„์ด๋‹ค.

{# template #}
{% for question in last_question_list %}
    <li><a href="/polls/{{question.id}}/">{{ question.question_text}}</a></li>
{% endfor %}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋“œ์ฝ”๋”ฉ๋œ ๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

ํ•˜์ง€๋งŒ project์— ์—ฌ๋Ÿฌ๊ฐœ์˜ app์ด ์กด์žฌํ•˜๊ณ , ๊ฐ™์€ path name์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. URLconf์— namespace๋ฅผ ์ถ”๊ฐ€ ํ•จ์œผ๋กœ์จ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

# 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/index.html #}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text}}</a></li>

์ฐธ์กฐ

Last updated