Django路由配置之子路由include(URL分发)
子路由include(URL分发)
在一个项目中可能存在多个应用,为了方便区分和管理,在项目的总路由urls.py中会进行路由分发:
(1)项目总路由导入from django.conf.urls import url,include
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^app02/', include('app02.urls')),
# 可以包含其他的URLconfs文件,注意不需要写.py后缀
url(r'^app01/',include('app01.urls')),
#别忘了要去app01这个应用下创建一个urls.py的文件,现在的意思是凡是以app01开头的路径请求,都让它去找app01下的urls文件中去找对应的视图函数,还要注意一点,此时这个文件里面的那个app01路径不能用$结尾,因为如果写了$,就没办法比配上app01/后面的路径了
]
(2)子应用中创建自己的分路由urls.py,进行URLConf配置
App01:
from django.conf.urls import url
from app01 import views
urlpatters=[
url(r'^index/', views.index),
]
App02:
from django.conf.urls import url
from app02 import views
urlpatters=[
url(r'^index/', views.index),
]