๐Ÿ”ฅ ์žฅ๊ณ  ํŠœํ† ๋ฆฌ์–ผ ์ง„ํ–‰ํ•ด๋ณด๊ธฐ3

may_soouuยท2020๋…„ 8์›” 27์ผ
1

Django

๋ชฉ๋ก ๋ณด๊ธฐ
5/9

ํŠœํ† ๋ฆฌ์–ผ 3์—์„œ๋Š” ๋ทฐ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์ค‘์ ์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.

1. view, url ์ถ”๊ฐ€ํ•˜๊ธฐ

polls / views.py
 # ์œ„์˜ ๊ฒฝ๋กœ๋กœ ๊ฐ€์„œ ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€ํ•˜๊ธฐ
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)

์ด ํ›„, ์ด view๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด 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/urls.py ์˜ urlpatterns๋ฅผ ํ•œ๋ฒˆ ๋ณด์ž
๊ณผ๊ฑฐ์—๋Š” ์ •๊ทœํ‘œํ˜„์‹(์ •๊ทœํ‘œํ˜„์‹)์œผ๋กœ ๊ตฌ์„ฑํ–ˆ์—ˆ๋‹ค.

์š”์ฆ˜์—๋Š” ์ •๊ทœํ‘œํ˜„์‹ ๋ง๊ณ  import path ๋ฅผ ํ•˜์—ฌ path ๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.
๐Ÿ‘Š๐Ÿป ์ •๊ทœํ‘œํ˜„์‹์ด ์ง๊ด€์ ์ด์ง€ ์•Š๊ณ  ๋„ˆ๋ฌด ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์„œ path๋ฅผ
๋” ์ง€ํ–ฅํ•˜๋Š” ํŽธ !

2. index view ์—์„œ datebase ์ฝ๊ธฐ

๊ฐ ๋ทฐ๋Š” ๋‘๊ฐ€์ง€๋กœ ๋ณด์—ฌ์งˆ ์ˆ˜ ์žˆ๋‹ค.

  1. ์ฝ˜ํ…์ธ ๊ฐ€ ์žˆ๋Š” httpresponse(httpresponse์— ๋Œ€ํ•ด์„  ๋”ฐ๋กœ ๋‹ค๋ค„๋ณด์ž)
  2. http404 ์™€ ๊ฐ™์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
polls/views.py
 # ์œ„์˜ ๊ฒฝ๋กœ๋กœ ๊ฐ€์„œ index ๋ถ€๋ถ„์„ ์•„๋ž˜ ๋‚ด์šฉ์œผ๋กœ ์ˆ˜์ •ํ•˜์ž

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

question ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์™€์„œ, index view์— ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ, pub_date์˜ ์—ญ์ˆœ์œผ๋กœ 5๊ฐœ๊นŒ์ง€๋งŒ ์ •๋ ฌํ•œ ๊ฐ’์„ lastes_questin_list
๐Ÿคทโ€โ™‚๏ธ ๊ถ๊ธˆ์ฆ,, > [-1:-5] ๋ณด๋‹ค ๋” ๋ณด๊ธฐ ์ข‹์•„์„œ pub_date์— ๋งˆ์ด๋„ˆ์Šค๋ฅผ ๋ถ™์ธ๊ฑธ๊นŒ?!

์ง€๊ธˆ๊นŒ์ง€ ํŽ˜์ด์ง€๋ฅผ ๊พธ๋ฏธ์ง€์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์˜๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.
์žฅ๊ณ ์—์„œ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๋ผ๋Š” ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
๋ธŒ๋ผ์šฐ์ €๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— HTML์„ ๋„ฃ์„์ˆ˜๊ฐ€ ์—†๋‹ค.
ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋Š” ํŒŒ์ด์ฌ์„ HTML๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด, ๋™์ ์ธ ์›น ์‚ฌ์ดํŠธ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

3. ํ…œํ”Œ๋ฆฟ ๊พธ๋ฏธ๊ธฐ

ํ…œํ”Œ๋ฆฟ์„ ์ถ”๊ฐ€ํ•ด์„œ ๊พธ๋ฉฐ๋ณด์ž!!
์šฐ์„ , polls app ๋””๋ ‰ํ† ๋ฆฌ์— templates ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— polls ๋””๋ ‰ํ† ๋ฆฌ ๋˜ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ์•ˆ์— index.html ํ…œํ”Œ๋ฆฟ์„ ์ƒ์„ฑํ•˜์ž.

polls/templates/polls/index.html

 # ์œ„ ๊ฒฝ๋กœ์—์„œ ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€
{% if latest_question_list %}
   <ul>
   {% for question in latest_question_list %}
       <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
   {% endfor %}
   </ul>
{% else %}
   <p>No polls are available.</p>
{% endif %}

ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ์„ ์œ„ํ•ด index view ์ˆ˜์ •ํ•˜๊ธฐ

polls/views.py
 # ์œ„ ๊ฒฝ๋กœ์—์„œ ์•„๋ž˜ ๋‚ด์šฉ์œผ๋กœ index ์ˆ˜์ •ํ•˜๊ธฐ
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))

4. 404 ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌํ•˜๊ธฐ

question ์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด 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)
    return render(request, 'polls/detail.html', {'question': question})

get_object_or_404 ๋Š” http404์˜ ๊ด€์šฉ๊ตฌ์ด๋‹ค.
์˜ˆ์™ธ ๋‚ด์šฉ์„ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ „ํ•ด์ฃผ์ž

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>

์—ฌ๊ธฐ์„œ question.choice_set.all ์€ ํŒŒ์ด์ฌ ์ฝ”๋“œ์˜ question.choice_set.all() ํ•จ์ˆ˜์™€ ๋™์ผํ•˜๋‹ค.
ํ•ด๋‹น Quetion ๋ชจ๋ธ์„ foreign key ๋กœ ๊ฐ–๋Š” choice ๋ชจ๋ธ์„ for๋ฌธ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๐Ÿคทโ€โ™‚๏ธ foreign key

5. app_name

ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ ์•ˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์•ฑ์ด ์กด์žฌํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ๊ฐ ์•ฑ์— detail.html ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, ์–ด๋Š ์•ฑ์— ์žˆ๋Š” detail.html ์ธ์ง€ ์•Œ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด app_name์ด๋‹ค.

polls / urls.py ๋กœ ๊ฐ€๋ณด์ž

์ค‘๊ฐ„์— app_name ์„ ๋„ฃ์–ด์ฃผ์ž !

๊ทธ๋ฆฌ๊ณ  poll / templates / polls / index.html ์—์„œ๋„ ์•ฑ ๋„ค์ž„์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

profile
back-end ๊ฐœ๋ฐœ์ž

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