Django之程-多路由配置
在匹配输入时交给其它文件来处理,结构如下
mysite-
|
|--cmdb1
| |
| |--views
| |--urls
|--cmdb2
| |
| |--views
| |--urls
|--mysite(默认创建)
| |
| |--settings
| |--urls
分别在cmdb1\cmdb2的app内创建urls文件,同时修改views文件
cmdb1---urls
from . import views from django.urls import path urlpatterns = [ path('',views.cb1) ]
cmdb1---views
from django.http import HttpResponse def cb1(request): return HttpResponse('cb1_test_page')
cmdb2---urls
from . import views from django.urls import path urlpatterns = [ path('',views.cb2) ]
cmdb2---views
from django.http import HttpResponse def cb2(request): return HttpResponse('cb2_test_page')
最后修改mysite的urls文件
from django.urls import path,include urlpatterns = [ path('cb1/',include('cmdb1.urls')), path('cb2/',include('cmdb2.urls')), ]
此时如果访问127.0.0.1:8000/cb1时就会路由到cmdb1下url来匹配,
访问127.0.0.1:8000/cb2时就会路由到cmdb2下url来匹配