在多个app之间,有可能产生同名的url,这个时候避免反转url的时候混淆,可以使用应用命名空间做区分。应用命名空间使用。在应用url中定义 app_name;
下面通过两个案例说明
#python manage.py startapp app01
#python manage.py startapp app02
from django.contrib import admin
from django.conf.urls import url, include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('app01/', include('app01.urls')),
path('app02/', include('app02.urls')),
]
from django.urls import path
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('login', views.login,name='login'),
]
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect,reverse
# Create your views here.
def index(request):
if request.GET.get("username"):
return HttpResponse("front page app01!")
else:
return redirect(reverse("login"))
def login(request):
return HttpResponse("Login page app01!")
from django.urls import path
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('login', views.login,name='login'),
]
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, reverse
# Create your views here.
def index(request):
if request.GET.get("username"):
return HttpResponse("front page app02!")
else:
return redirect(reverse("login"))
def login(request):
return HttpResponse("Login page app02!")
我们可以看到每个app下面的urls都定义了name=index和name=login。 而每个views下面的reverse都是login。这个时候django回去找谁呢? 当我们浏览器打开路径: http://127.0.0.1:8000/app01 的时候,我们会发现跳转异常,竟然跳转到地址: http://127.0.0.1:8000/app02/login 上面。
这个时候我们使用命名空间app_name,就可以很好解决这个问题。
from django.urls import path
from . import views
#命名空间
app_name = 'app01' #这里变化了
urlpatterns = [
path('', views.index,name='index'),
path('login', views.login,name='login'),
]
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect,reverse
# Create your views here.
def index(request):
if request.GET.get("username"):
return HttpResponse("front page app01!")
else:
return redirect(reverse("app01:login")) #这里变化了
def login(request):
return HttpResponse("Login page app01!")
from django.urls import path
from . import views
app_name = 'app02' #这里变化了
urlpatterns = [
path('', views.index,name='index'),
path('login', views.login,name='login'),
]
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, reverse
# Create your views here.
def index(request):
if request.GET.get("username"):
return HttpResponse("front page app02!")
else:
return redirect(reverse("app02:login")) #这里变化了
def login(request):
return HttpResponse("Login page app02!")
当我们浏览器打开路径: http://127.0.0.1:8000/app01 的时候,就会正常跳转地址: http://127.0.0.1:8000/app01/login 了
from django.contrib import admin
from django.conf.urls import url, include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('book1/', include('book.urls')),
path('book2/', include('book.urls')),
]
多个url,指向同一个app。
from django.urls import path
from . import views
urlpatterns = [
path('', views.book_list),
path('login', views.book_login, name="login"),
]
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from django.shortcuts import redirect,reverse
def book_list(request):
if request.GET.get("username"):
return HttpResponse("My book list !!!!")
else:
return redirect(reverse("login"))
def book_login(request):
return HttpResponse("Please Login!!!!")
通过上面案例,我们可以知道。
当执行http://127.0.0.1:8000/book2/ 的时候就会跳转http://127.0.0.1:8000/book2/login
当执行http://127.0.0.1:8000/book1/ 的时候发现还是跳转http://127.0.0.1:8000/book2/login
from django.contrib import admin
from django.conf.urls import url, include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('book1/', include('book.urls'), namespace='book1')), #变更部分
path('book2/', include('book.urls'), namespace='book2')), #变更部分
]
from django.urls import path
from . import views
app_name = "book"
urlpatterns = [
path('', views.book_list),
path('login', views.book_login, name="login"),
]
def book_list(request):
#获取当前namespace名称。
current_namespace = request.resolver_match.namespace #变更部分
if request.GET.get("username"):
return HttpResponse("My book list !!!!")
else:
print(current_namespace)
#动态返回命名空间信息
return redirect(reverse("%s:login"% current_namespace)) #变更部分
def book_login(request):
return HttpResponse("Please Login!!!!")
(1)include(module,namespace=Node)
(2)include((pattern_list,app_namesapace),namespace=None)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。