django的url匹配
学习网站:官方
以下是个人摘录:
1.基本匹配
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‘), )
/articles/2005/03/ 这个匹配第三个url,即news.views.mouth_archive(2005, 03)
/articles/2003/03/03/ 匹配第4个,即 news.views.article_detail(request, ‘2003‘, ‘03‘, ‘03‘).
2.关键字匹配
urlpatterns = patterns(‘‘, url(r‘^articles/2003/$‘, ‘news.views.special_case_2003‘), url(r‘^articles/(?P<year>\d{4})/$‘, ‘news.views.year_archive‘), url(r‘^articles/(?P<year>\d{4})/(?P<month>\d{2})/$‘, ‘news.views.month_archive‘), url(r‘^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$‘, ‘news.views.article_detail‘), )
/articles/2005/03/ 即news.views.month_archive(request, year=‘2005‘, month=‘03‘),代替了news.views.month_archive(request, ‘2005‘, ‘03‘).
这里是不是可以把url各个参数都获取出来呢?当然
3.默认值匹配
urlpatterns = patterns(‘‘, url(r‘^blog/$‘, ‘blog.views.page‘), url(r‘^blog/page(?P<num>\d+)/$‘, ‘blog.views.page‘), ) # View (in blog/views.py) def page(request, num="1"): # Output the appropriate page of blog entries, according to num. ...
如果是第一个匹配到了,那么num会默认为1
4.模型
假如:
urlpatterns = patterns(‘‘, 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‘), )
则可代替为:
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‘), )
注意patterns第一个参数变化
5.多模型
假如:
urlpatterns = patterns(‘‘, url(r‘^$‘, ‘myapp.views.app_index‘), url(r‘^(?P<year>\d{4})/(?P<month>[a-z]{3})/$‘, ‘myapp.views.month_display‘), url(r‘^tag/(?P<tag>\w+)/$‘, ‘weblog.views.tag‘), )
则可代替为:
urlpatterns = patterns(‘myapp.views‘, url(r‘^$‘, ‘app_index‘), url(r‘^(?P<year>\d{4})/(?P<month>[a-z]{3})/$‘,‘month_display‘), ) urlpatterns += patterns(‘weblog.views‘, url(r‘^tag/(?P<tag>\w+)/$‘, ‘tag‘), )
6.include
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 ... )
下面这个用法也可以:
extra_patterns = patterns(‘‘, url(r‘^reports/(?P<id>\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)), )
下面这个是不是很复杂?
urlpatterns = patterns(‘wiki.views‘, url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$‘, ‘history‘), url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$‘, ‘edit‘), url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$‘, ‘discuss‘), url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$‘, ‘permissions‘), )
在替换下吧:
urlpatterns = patterns(‘‘, url(r‘^(?P<page_slug>\w+)-(?P<page_id>\w+)/‘, include(patterns(‘wiki.views‘, url(r‘^history/$‘, ‘history‘), url(r‘^edit/$‘, ‘edit‘), url(r‘^discuss/$‘, ‘discuss‘), url(r‘^permissions/$‘, ‘permissions‘), ))), )
7.额外参数
urlpatterns = patterns(‘blog.views‘, url(r‘^blog/(?P<year>\d{4})/$‘, ‘year_archive‘, {‘foo‘: ‘bar‘}), )
/blog/2005/,即blog.views.year_archive(request, year=‘2005‘, foo=‘bar‘).
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。