您好,登錄后才能下訂單哦!
在多個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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。