아래와 같이 뷰를 추가해보자.
기존의 것과 다른 점은 인수를 가진다는 점이다.
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at the question %s." % question_id)
def results(request, question_id):
response = "You're looking at the result of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
# Create your views here.
위에서 아래와 같이 url() 호출을 통해 추가한 뷰를 polls.urls 에 넣는다.
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name = 'detail' ),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
이후 웹브라우저에서 아래와 같이 실행해보면 views.py 에 추가한대로 메시지가 호출됨을 볼 수 있다.
http://117.52.74.103:8000/polls/1/
You're looking at the question 1.
http://117.52.74.103:8000/polls/1/vote/
You're voting on question 1.
http://117.52.74.103:8000/polls/1/results/
You're looking at the result of question 1.
'프로그래밍 Programming' 카테고리의 다른 글
[django] Write views that actually do something (0) | 2015.07.17 |
---|---|
장고 URLS 에서 달러($)와 캐럿(^)이 의미하는 것은? (0) | 2015.07.17 |
[django] admin 계정 패스워드 리셋하는 방법 (0) | 2015.07.16 |
[django] Write your first view (0) | 2015.07.13 |
[django] Customize the admin look and feel (0) | 2015.07.13 |