1 概述
在常见的博客首页中, 可能会出现
首页: 显示最近的几个条目
详细
基于年, 月, 日的存档页面
评论
因此基于此, 我们可能需要
index detail results vote针对性的视图
2 编写视图
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)
添加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‘), ]
可以看到
/polls/34/ 将访问 detail() /polls/34/results/ 将访问 results() /polls/34/vote/ 将访问 vote()
当有人输入/polls/34/访问时, 整个过程是这个样的
Django先健在mysite.urls这个Python模块, 因为ROOT_URL_CONF设置指向的
然后找到其中的urlpatterns并按照顺序遍历正则表达式
时间: 2024-10-10 10:13:10