如何在Django中使用url-创新互联
这篇文章将为大家详细讲解有关如何在Django中使用url,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
成都创新互联,专注为中小企业提供官网建设、营销型网站制作、响应式网站开发、展示型成都网站设计、成都做网站等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。最简单的形式
from django.conf.urls import patterns, 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'),
)
其中,正则表达式中组匹配出来的结果可以作为positional parameters传递给view.
如果url是www.yourdomain/articles/2005/,则会匹配第二条规则,执行news.views.year_archive('2005').
注意点
域名部分会被过滤掉
articles的前面不需要添加/,因为前序url的末尾一定会有/
任何组匹配的变量,都会议字符串的形式传递给view, 虽然通过(\d{4})匹配出了2005,但2005任然会被当做字符串传递给year_archive
利用named group来传递参数
可以通过以下形式为特定的组指定一个名称.
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'), )
这样的话,组的匹配结果会通过keyword parameters的形式传递给view.例如year_archive(year='2005')
利用named group可以为view指定一个默认参数来匹配多条规则。
# URLconf from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^blog/$', 'blog.views.page'), url(r'^blog/page(?P\d+)/$', 'blog.views.page'), ) # View (in blog/views.py) def page(request, num="1"): # Output the appropriate page of blog entries, according to num.
指定view前缀(提取公因式)
patterns函数的第一个参数即是view的前缀
from django.conf.urls import patterns, 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其它匹配模块
from django.conf.urls import include, 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
from django.conf.urls import include, 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函数传递额外参数
from django.conf.urls import patterns, url urlpatterns = patterns('blog.views', url(r'^blog/(?P\d{4})/$', 'year_archive', {'foo': 'bar'}), )
直接使用view函数
from django.conf.urls import patterns, url from mysite.views import archive, about, contact urlpatterns = patterns('', url(r'^archive/$', archive), url(r'^about/$', about), url(r'^contact/$', contact), )
关于如何在Django中使用url就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:如何在Django中使用url-创新互联
文章网址:http://ybzwz.com/article/ehphg.html