之前的路由分发书写:
djangoProject/urls.py
from django.urls import include, re_path urlpatterns = [ re_path(r'^web/', include(("app01.urls", "app01"))), ]
app01/urls.py
from django.urls import re_path from app01 import views urlpatterns = [ re_path(r'^index/$', views.index), re_path(r'^home/$', views.home), ]
app01/views.py
from django.shortcuts import render, HttpResponse # Create your views here. def index(request): return HttpResponse("index") def home(request): return HttpResponse("home")
查看include源码
def include(arg, namespace=None): app_name = None if isinstance(arg, tuple): # Callable returning a namespace hint. try: urlconf_module, app_name = arg except ValueError: if namespace: raise ImproperlyConfigured( 'Cannot override the namespace for a dynamic module that ' 'provides a namespace.' ) raise ImproperlyConfigured( 'Passing a %d-tuple to include() is not supported. Pass a ' '2-tuple containing the list of patterns and app_name, and ' 'provide the namespace argument to include() instead.' % len(arg) ) else: # No namespace hint - use manually provided namespace. urlconf_module = arg if isinstance(urlconf_module, str): urlconf_module = import_module(urlconf_module) patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module) app_name = getattr(urlconf_module, 'app_name', app_name) if namespace and not app_name: raise ImproperlyConfigured( 'Specifying a namespace in include() without providing an app_name ' 'is not supported. Set the app_name attribute in the included ' 'module, or pass a 2-tuple containing the list of patterns and ' 'app_name instead.', ) namespace = namespace or app_name # Make sure the patterns can be iterated through (without this, some # testcases will break). if isinstance(patterns, (list, tuple)): for url_pattern in patterns: pattern = getattr(url_pattern, 'pattern', None) if isinstance(pattern, LocalePrefixPattern): raise ImproperlyConfigured( 'Using i18n_patterns in an included URLconf is not allowed.' ) return (urlconf_module, app_name, namespace) # 返回值是一个有三个元素的元组,urlconf_module会等于'app01.urls',urlconf_module = import_module(urlconf_module)相当于urlconf_module会等于from app01 import urls,也就是说urlconf_module会等于一个文件对象,即urls
路由分发第二种写法
from django.urls import re_path from app01 import urls urlpatterns = [ path('admin/', admin.site.urls), re_path(r'^web/', (urls, app_name, namespace)), ]
在路由分发include中打上断点,以debug模式启动Django,查看其调用栈
结论:
# 如果第一个参数有urls.urlpatterns属性,那么子路由就从该属性中获取 # 如果第一个参数无urls.urlpatterns属性,那么子路由就是第一个参数
路由第三种写法
djangoProject/urls.py
from django.urls import re_path from app01 import views urlpatterns = [ re_path(r'^web/', ([ re_path(r'^index/$', views.index), re_path(r'^home/$', views.home), ], None, None)) # 可删除app01下的urls.py文件 ]
正常启动Django,前端输入路由查看是否运行正常