在完成(2)之后,我们来继续关注创建一个公开的界面——views
view是在你的Django网页中的实现特定功能大的页面,它有一个特殊的模板,比如,在一个博客app中,你可能需要以下的views:
Blog homepage
Entry “detail” page
Year-based archive page
Month-based archive page
Day-based archive page
Comment action
在polls应用中,我们有下面4个views
index
detail
results
vote
写更多的views
修改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)
修改polls.urls:
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'),
]