ROOT_URLCONF = <span class="hljs-string">'project.urls'</span>
path() 对象的作用
在主路由中主要是引入其他子路由模块
基本使用
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path, include
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin
urlpatterns = [
path(<span class="hljs-string">r'admin/'</span>, admin.site.urls),
path(<span class="hljs-string">r''</span>, include(<span class="hljs-string">'App.urls'</span>)),
]
include()函数
作用:找到子路由模块
参数
带命名空间
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path, include
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin
urlpatterns = [
path(<span class="hljs-string">r'admin/'</span>, admin.site.urls),
path(<span class="hljs-string">r''</span>, include((<span class="hljs-string">'App.urls'</span>, <span class="hljs-string">"App"</span>),namespace=<span class="hljs-string">"App"</span>)),
]
多个子路由模块
<span class="hljs-comment"># 假设有多个应用</span>
<span class="hljs-comment"># http://www.sunck.wang:8000/App/index1</span>
<span class="hljs-comment"># 匹配 App/index1 App3/home2</span>
<span class="hljs-comment"># path(r'App/', include('App.urls')),</span>
<span class="hljs-comment"># path(r'App1/', include('App1.urls')),</span>
<span class="hljs-comment"># path(r'App2/', include('App2.urls')),</span>
<span class="hljs-comment"># path(r'App3/', include('App3.urls')),</span>
目录结构
project/
App/
urls.py <span class="hljs-comment"># 自定义urls.py文件</span>
project/
urls.py
from django.urls import path, re_path