Django的路由分发与名称空间

时间:2023-12-11 22:09:50
路由分发
django每一个app下面都可以有自己的urls.py路由层,templates文件夹,static文件夹
项目名下urls.py(总路由)不再做路由与视图函数的匹配关系而是做路由的分发
from django.conf.urls import include # 路由分发 注意路由分发总路由千万不要$结尾
url(r'^app01/',include(app01_urls)),
url(r'^app02/',include(app02_urls)) # 在应用下新建urls.py文件,在该文件内写路由与视图函数的对应关系即可
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^index/',views.index)
] 名称空间(了解)
url(r'^app01/',include(app01_urls,namespace='app01')),
url(r'^app02/',include(app02_urls,namespace='app02'))
  或
     url(r'^app01/' include(app01.urls),
app01.urls.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^index/',views.index,name='index')
]
      #或取名字时取独一无二的,这样就不会有冲突 app02.urls.py
from django.conf.urls import url
from app02 import views
urlpatterns = [
url(r'^index/',views.index,name='index')
] app01.views.py
reverse('app01:index') app02.views.py
reverse('app02:index')