码迷,mamicode.com
首页 > Web开发 > 详细

Django URL管理--include()

时间:2016-04-19 10:10:28      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

(1)Including other URLconfs

比如一个website项目urls.py, include了其他的urls:

from django.conf.urls import include, url

urlpatterns = [
    # ... snip ...
    url(r‘^community/‘, include(‘django_website.aggregator.urls‘)),
    url(r‘^contact/‘, include(‘django_website.contact.urls‘)),
    # ... snip ...
]

  注意include之前的正则表达式没有终止符 $ 而是 /   

     当调用clude时,把url前边正则表达式匹配到的chop off(砍掉),剩下的string传递到include的urls中进行进一步操作。

 

另一个例子

from django.conf.urls import include, url

from apps.main import views as main_views
from credit import views as credit_views

extra_patterns = [
    url(r‘^reports/$‘, credit_views.report),
    url(r‘^reports/(?P<id>[0-9]+)/$‘, credit_views.report),
    url(r‘^charge/$‘, credit_views.charge),
]

urlpatterns = [
    url(r‘^$‘, main_views.homepage),
    url(r‘^help/‘, include(‘apps.help.urls‘)),
    url(r‘^credit/‘, include(extra_patterns)),
]

    该例中 url:   credit/reports/    对应的方法是credit_views.report

 

Django URL管理--include()

标签:

原文地址:http://www.cnblogs.com/LuoDa/p/5406912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!