您好,登錄后才能下訂單哦!
在多個app之間,有可能產(chǎn)生同名的url,這個時候避免反轉(zhuǎn)url的時候混淆,可以使用應(yīng)用命名空間做區(qū)分。應(yīng)用命名空間使用。在應(yīng)用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回去找誰呢? 當我們?yōu)g覽器打開路徑: http://127.0.0.1:8000/app01 的時候,我們會發(fā)現(xiàn)跳轉(zhuǎn)異常,竟然跳轉(zhuǎn)到地址: 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!")
當我們?yōu)g覽器打開路徑: http://127.0.0.1:8000/app01 的時候,就會正常跳轉(zhuǎn)地址: 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!!!!")
通過上面案例,我們可以知道。
當執(zhí)行http://127.0.0.1:8000/book2/ 的時候就會跳轉(zhuǎn)http://127.0.0.1:8000/book2/login
當執(zhí)行http://127.0.0.1:8000/book1/ 的時候發(fā)現(xiàn)還是跳轉(zhuǎn)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)
#動態(tài)返回命名空間信息
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)
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。