์ฒซ ๋ฒ์งธ ์ฅ๊ณ ์ฑ ์์ฑํ๊ธฐ Part 3
์ด ํํ ๋ฆฌ์ผ์ ํํ ๋ฆฌ์ผ Part 2์์ ์ด์ด์ง๋๋ค.
๋ทฐ
๋ทฐ๋ Django ์ดํ๋ฆฌ์ผ์ด์ ์ด ์ผ๋ฐ์ ์ผ๋ก ํน์ ๊ธฐ๋ฅ๊ณผ ํฌํ๋ฆฟ์ ์ ๊ณตํ๋ ์น ํ์ด์ง์ ํ ์ข ๋ฅ์ ๋๋ค.
์ฐ๋ฆฌ๊ฐ ๋ง๋๋ poll์ดํ๋ฆฌ์ผ์ด์ ์์ ๋ค์๊ณผ ๊ฐ์ ๋ค๊ฐ์ view๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
- ์ง๋ฌธ "์์ธ" ํ์ด์ง -- ์ต๊ทผ์ ์ง๋ฌธ๋ค์ ํ์ํฉ๋๋ค.
- ์ง๋ฌธ "์ธ๋ถ" ํ์ด์ง -- ์ง๋ฌธ ๋ด์ฉ๊ณผ, ํฌํํ ์ ์๋ ์์์ ํ์ํฉ๋๋ค.
- ์ง๋ฌธ "๊ฒฐ๊ณผ" ํ์ด์ง -- ํน์ ์ง๋ฌธ์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ํ์ํฉ๋๋ค
- ํฌํ ๊ธฐ๋ฅ -- ํน์ ์ง๋ฌธ์ ๋ํด ํน์ ์ ํ์ ํ ์ ์๋ ํฌํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
๋ทฐ ์ถ๊ฐํ๊ธฐ
์ด์ polls/views.py์ ๋ทฐ๋ฅผ ์ถ๊ฐํด ๋ด ์๋ค.
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)
"You're voting on question %s." % question_id ๋ python์ ๋ฌธ์์ด ํฌ๋งทํ ๋ฐฉ์์ ๋๋ค.
python ๋ฌธ์์ด ํฌ๋งทํ ๋ฐฉ์์ ์ด๊ณณ ์์ ์ค๋ช ํ๊ณ ์์ต๋๋ค.
HttpResponse ํจ์๋ django.http๋ชจ๋์์ ์ ๊ณตํ๋ ํจ์ ์ ๋๋ค.
HttpResponse ํจ์์ ๋ํด์ ์๊ณ ์ถ๋ค๋ฉด
Google ๊ฒ์์์ง์ ํตํด [Django HttpResponse function] ๋ผ๊ณ ๊ฒ์ํด ๋ด ์๋ค.
์ด์ ๋ทฐ๋ฅผ ์ถ๊ฐํ์ผ๋ path(๊ฒฝ๋ก)๋ฅผ ์ค์ ํด์ผํฉ๋๋ค.
path(๊ฒฝ๋ก)๋ฅผ ์ค์ ํ๋ ค๋ฉด polls/urls.py์ ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
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'),
]
์์ ์ ๋ธ๋ผ์ฐ์ URL์ "http://127.0.0.1:8000/polls/2/"๋ฅผ ์ ๋ ฅํ๋ฉด ํ์ด์ง๊ฐ ์ ์์ ์ผ๋ก ์๋ํ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์์ polls/views.py์ ์ถ๊ฐํ detail, results, vote ํจ์๋ค์ views๊ฐ์ฒด์ ํจ์๋ผ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
์ฌ์ฉ๋ฐฉ๋ฒ์ view.๋ฉ์๋๋ช ์ผ๋ก ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
์ฌ์ฉ์๊ฐ ์น ์ฌ์ดํธ์ ํ์ด์ง๋ฅผ ์์ฒญํ ๋ Django๋ mysite.urls ํ์ด์ฌ ๋ชจ๋์ ๋ถ๋ฌ์ค๊ฒ ๋ฉ๋๋ค.
์๋ฅผ๋ค์ด ์ฌ์ฉ์๊ฐ "/polls/20/"์ ์์ฒญํ๋ค๋ฉด
mysite.urls์์ urlpatterns๋ผ๋ ๋ณ์๋ฅผ ์ฐพ๊ณ , ์์๋๋ก ํจํด์ ๋ฐ๋ผ ๊ฐ๋๋ค.
'polls/'๋ฅผ ์ฐพ์ ํ์ ์ผ์นํ๋ ํ ์คํธ ("polls/")๋ฅผ ๋ฒ๋ฆฌ๊ณ ๋จ์ ํ ์คํธ ("20/") ๋ฅผ polls.urls์ URLconf๋ก
์ ๋ฌํ์ฌ ๋จ์ ์ฒ๋ฆฌ๋ฅผ ์งํํฉ๋๋ค.
'<int:question_id>/'์ ์ผ์นํ์ฌ, ๊ฒฐ๊ณผ์ ์ผ๋ก detail() ๋ทฐ ํจ์๊ฐ ํธ์ถ๋ฉ๋๋ค.
๋ทฐ์ ์๋ก์ด ๋ฌด์ธ๊ฐ๋ฅผ ํ๋๋ก ๋ง๋ค๊ธฐ
์ด์ ๋ทฐ์๋ Python ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ๋ฌด์์ด๋ ํ ์ ์์ต๋๋ค.
PDF๋ฅผ ์์ฑํ๊ฑฐ๋, XML๋ฅผ ์ถ๋ ฅํ๊ฑฐ๋, ์ค์๊ฐ์ผ๋ก ZIPํ์ผ์ ๋ง๋ค ์ ์์ต๋๋ค.
๊ทธ๋ผ ์ด์ index() ๋ทฐ๋ฅผ ํธ์ถํ ๋ ์ต์ํ 5๊ฐ์ ํฌํ์ง๋ฌธ์ ๋ณด์ด๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค.
polls/views.py
--------------------------------------------------------
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)
polls/views.py์ detail, results, vote ํจ์๋ ๋๋๊ณ
models ๋ชจ๋์ importํด์ฃผ๊ณ index()ํจ์๋ ์์ ํด์ค๋๋ค.
ํ ํ๋ฆฟ ์์คํ
Python์ฝ๋๋ก๋ถํฐ ๋์์ธ์ ๋ถ๋ฆฌํ๋๋ก Django์ ํ ํ๋ฆฟ ์์คํ ์ ์ฌ์ฉํด ๋ด ์๋ค.
polls๋๋ ํฐ๋ฆฌ์ ๋ค์๊ณผ ๊ฐ์ ํ์ผ์ ๋ง๋ญ๋๋ค.
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))
context ๋ ํ ํ๋ฆฟ์์ ์ฐ์ด๋ ๋ณ์๋ช ๊ณผ python ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐํ๋ ์ฌ์ ํ ๊ฐ์ ๋๋ค.
render() ํจ์
HttpResponse ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ตฌ๋ฌธ์ ์์ฃผ ์ฐ๋ ๋ฌธ๋ฒ์ ๋๋ค.
Django๋ ์ด ํํ์ ๋จ์ถํ ์ ์๋ render()ํจ์๋ฅผ ์ ๊ณตํฉ๋๋ค.
index() ํจ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์์ต๋๋ค.
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)
๋ชจ๋ ๋ทฐ์ ์ ์ฉํ๋ค๋ฉด, ๋ ์ด์ loader ์ HttpResponse๋ฅผ ์ํฌํธํ์ง ์์๋ ๋ฉ๋๋ค.
render()ํจ์๋ request ๊ฐ์ฒด๋ฅผ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ๋ฐ๊ณ , ํ ํ๋ฆฟ ์ด๋ฆ์ ๋๋ฒ์งธ ์ธ์๋ก ๋ฐ์ผ๋ฉฐ
context ์ฌ์ ํ ๊ฐ์ฒด๋ฅผ ์ ํ์ ์ธ์๋ก ๋ฐ์ต๋๋ค.
์ธ์๋ก ์ง์ ๋ context๋ก ํํ๋ ํ ํ๋ฆฟ์ HttpResponse ๊ฐ์ฒด๊ฐ ๋ฐํ๋ฉ๋๋ค.
'Web > ๐ Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Django ํ ํ๋ฆฟ ์ธ์ด (0) | 2020.06.10 |
---|---|
Django ํํ ๋ฆฌ์ผ (3) (0) | 2020.06.09 |
Django ํํ ๋ฆฌ์ผ (2) (0) | 2020.06.09 |
Django ํํ ๋ฆฌ์ผ (1) (0) | 2020.06.09 |