Django URL管理--include()

(1)Including other URLconfspython

好比一個website項目urls.py, include了其餘的urls:web

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中進行進一步操做。django

 

另外一個例子app

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.reporturl

相關文章
相關標籤/搜索