django学习2 视图和模板

1 编写更多的视图

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.py文件中加入下面的url模式,将其映射到我们上面新增的视图。

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'),
]

 

访问浏览器:http://127.0.0.1:8000/polls/34/

image

posted @ 2017-09-25 18:48  绿色的麦田  阅读(232)  评论(0编辑  收藏  举报