对Django url的几种使用方式详解
利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护。
最简单的形式
fromdjango.conf.urlsimportpatterns,url
urlpatterns=patterns('',
url(r'^articles/2003/$','news.views.special_case_2003'),
url(r'^articles/(\d{4})/$','news.views.year_archive'),
url(r'^articles/(\d{4})/(\d{2})/$','news.views.month_archive'),
url(r'^articles/(\d{4})/(\d{2})/(\d+)/$','news.views.article_detail'),
)
其中,正则表达式中组匹配出来的结果可以作为positionalparameters传递给view.
如果url是www.yourdomain/articles/2005/,则会匹配第二条规则,执行news.views.year_archive('2005').
注意点
域名部分会被过滤掉
articles的前面不需要添加/,因为前序url的末尾一定会有/
任何组匹配的变量,都会议字符串的形式传递给view,虽然通过(\d{4})匹配出了2005,但2005任然会被当做字符串传递给year_archive
利用namedgroup来传递参数
可以通过以下形式为特定的组指定一个名称.
urlpatterns=patterns('', url(r'^articles/2003/$','news.views.special_case_2003'), url(r'^articles/(?P\d{4})/$','news.views.year_archive'), url(r'^articles/(?P \d{4})/(?P \d{2})/$','news.views.month_archive'), url(r'^articles/(?P \d{4})/(?P \d{2})/(?P \d{2})/$','news.views.article_detail'), )
这样的话,组的匹配结果会通过keywordparameters的形式传递给view.例如year_archive(year='2005')
利用namedgroup可以为view指定一个默认参数来匹配多条规则。
#URLconf fromdjango.conf.urlsimportpatterns,url urlpatterns=patterns('', url(r'^blog/$','blog.views.page'), url(r'^blog/page(?P\d+)/$','blog.views.page'), ) #View(inblog/views.py) defpage(request,num="1"): #Outputtheappropriatepageofblogentries,accordingtonum.
指定view前缀(提取公因式)
patterns函数的第一个参数即是view的前缀
fromdjango.conf.urlsimportpatterns,url urlpatterns=patterns('news.views', url(r'^articles/(\d{4})/$','year_archive'), url(r'^articles/(\d{4})/(\d{2})/$','month_archive'), url(r'^articles/(\d{4})/(\d{2})/(\d+)/$','article_detail'), )
指定多个view前缀
urlpatterns=patterns('myapp.views', url(r'^$','app_index'), url(r'^(?P\d{4})/(?P [a-z]{3})/$','month_display'), ) urlpatterns+=patterns('weblog.views', url(r'^tag/(?P \w+)/$','tag'), )
include其它匹配模块
fromdjango.conf.urlsimportinclude,patterns,url urlpatterns=patterns('', #...snip... url(r'^comments/',include('django.contrib.comments.urls')), url(r'^community/',include('django_website.aggregator.urls')), url(r'^contact/',include('django_website.contact.urls')), #...snip... )
当然也可以直接include其它patterns
fromdjango.conf.urlsimportinclude,patterns,url extra_patterns=patterns('', url(r'^reports/(?P\d+)/$','credit.views.report'), url(r'^charge/$','credit.views.charge'), ) urlpatterns=patterns('', url(r'^$','apps.main.views.homepage'), url(r'^help/',include('apps.help.urls')), url(r'^credit/',include(extra_patterns)), )
为view函数传递额外参数
fromdjango.conf.urlsimportpatterns,url urlpatterns=patterns('blog.views', url(r'^blog/(?P\d{4})/$','year_archive',{'foo':'bar'}), )
直接使用view函数
fromdjango.conf.urlsimportpatterns,url frommysite.viewsimportarchive,about,contact urlpatterns=patterns('', url(r'^archive/$',archive), url(r'^about/$',about), url(r'^contact/$',contact), )
以上这篇对Djangourl的几种使用方式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。