django url 路由设置技巧

Django的url使用方法
利用Django开发站点。能够设计出很优美的url规则,假设url的匹配规则(包括正則表達式)组织得比較好,view的结构就会比較清晰。比較easy维护。
最简单的形式
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('').
注意点
域名部分会被过滤掉
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='') 利用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=""):
# 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', # 在第一个参数中直接指定到视图所在的py文件中, 当匹配的是方法视图时,直接写上方法视图即可
url(r'^$', 'app_index'),
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','month_display'),
)
urlpatterns += patterns(
  'weblog.views', # 指定第二个view文件, 路由到其中的视图
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),
)
上一篇:Java程序员的10道XML面试题


下一篇:django URL路由基础