URL Routing System - Namespaces

Namespaces

1. urs.py under Django

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')),


]

#Two ur 127.0.0.1/cmdb/ 127.0.0.1/app/ point to a url under cmdb at the same time

2. urls.py under cmdb in the project

from cmdb import views 
appname='cmdb' #This appname needs to be set urlpatterns
= [ path('login/', views.login,name="login"), ] #That is, the two urls 127.0.0.1/cmdb/login and 127.0.0.1/app/login both point to the login function in the views in cmdb

3. views.py in cmdb

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026973&siteId=291194637