django - nature (include) route distribution of

django - nature (include) route distribution of

Mode 1: General Usage

# django2.x中:还需要在app01.urls.py中添加:app_name = "app01"
from django.conf.urls import url,include

    urlpatterns = [
        url(r'^web/', include("app01.urls", namespace="web")),
    ]
  • At this point, it is necessary app01/urls.pyto specify app_name = "app01" file

Mode 2: the include source code returns the way

# include函数主要返回有三个元素的元组。
from django.conf.urls import url,include
from app01 import urls
urlpatterns = [
        url(r'^web/', (urls, app_name, namespace) ),
]
    
 # 第一个参数是urls文件对象,通过此对象可以获取urls.patterns获取分发的路由。

Mode 3: Django source code implementation of nature

# 在源码内部,读取路由时:
# 如有第一个参数有:urls.patterns 属性,那么子路由就从该属性中后去。
# 如果第一个参数无:urls.patterns 属性,那么子路由就是第一个参数。

from django.conf.urls import url

urlpatterns = [
        url(r'^web/', (
                            [
                                url(r'^index/', views.index),
                                url(r'^home/', views.home),
                            ], 
                            app_name, 
                            namespace
                      )
        ), 
]

# 第一个参数是urls文件对象,通过此对象可以获取urls.patterns获取分发的路由。

Guess you like

Origin www.cnblogs.com/liuxu2019/p/12114374.html