命名空间
1.工程Django下的urs.py
from django.conf.urls import url,include from django.urls import path,re_path urlpatterns = [ path('admin/', admin.site.urls), path('cmdb/', include("cmdb.urls",namespace='cmdb')),
path('app/', include("cmdb.urls",namespace='app')),
] #两个ur 127.0.0.1/cmdb/ 127.0.0.1/app/ 同时指向一个cmdb下的url
2.工程中cmdb下的urls.py
from cmdb import views
appname='cmdb' #这个appname需要设置 urlpatterns = [ path('login/', views.login,name="login"), ] #即两个url 127.0.0.1/cmdb/login 和 127.0.0.1/app/login同时指向 cmdb中views中的login函数
3.cmdb中views.py
from django.shortcuts import HttpResponse,render
from django.urls import reverse
def login(request): v=reverse('cmdb':'login') 根据namespace和name反生成url
print(v)
return HttpResponse('ok')
app01.views.py
1
2
3
|
def
detail(request, pk):
print
(request.resolver_match)
return
HttpResponse(pk)
|