对Django url的几种使用方式详解

yipeiwu_com5年前Python基础

利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护。

最简单的形式

<code> 
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'), 
)</code> 

其中,正则表达式中组匹配出来的结果可以作为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<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'), 
) 

这样的话,组的匹配结果会通过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<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. 

指定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<year>\d{4})/(?P<month>[a-z]{3})/$','month_display'), 
) 
urlpatterns += patterns('weblog.views', 
 url(r'^tag/(?P<tag>\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<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)), 
) 
 

为view函数传递额外参数

from django.conf.urls import patterns, url 
 
urlpatterns = patterns('blog.views', 
 url(r'^blog/(?P<year>\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的几种使用方式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3.5局部变量与全局变量作用域实例分析

本文实例讲述了Python3.5局部变量与全局变量作用域。分享给大家供大家参考,具体如下: 1、局部变量与全局变量定义: 在子程序(函数)中定义的变量称为:局部变量;在程序顶级(一开始)...

浅谈dataframe中更改列属性的方法

在读取文件时将整数变量读成了字符串, 或者需要转换列属性时,通过方法astype Python中 举例: dataframe.numbers=dataframe.numbers.as...

python获取文件后缀名及批量更新目录下文件后缀名的方法

本文实例讲述了python获取文件后缀名及批量更新目录下文件后缀名的方法。分享给大家供大家参考。具体实现方法如下: 1. 获取文件后缀名: 复制代码 代码如下:#!/usr/bin/py...

python从入门到精通 windows安装python图文教程

python从入门到精通 windows安装python图文教程

Python的官方介绍是: Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性...

python检查URL是否正常访问的小技巧

python检查URL是否正常访问的小技巧

今天,项目经理问我一个问题,问我这里有2000个URL要检查是否能正常打开,其实我是拒绝的,我知道因为要写代码了,正好学了点Python,一想,python处理起来容易,就选了pytho...