新建一個項目 app02
在 app02/ 下創建 urls.py:
from django.conf.urls import url from app02 import views urlpatterns = [ url(r'^blog/', views.test, name="blog"), ]
app01/urls.py:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^blog/', views.blog, name="blog"), ]
這兩個都有 blog/ 路徑,且都名為 blog,訪問的話就不知道該訪問哪一個
這時候需要用到命名空間
在 templates 目錄下創建 /books/blog.html 和 /news/blog.html
app01/views.py:
from django.shortcuts import render def test(request): return render(request, "test.html") def blog(request): return render(request, "news/blog.html") # news 前不要加 /
app02/views.py:
from django.shortcuts import render def test(request): return render(request, "books/blog.html") # books 前不要加 /
mysite2/urls.py:
from django.conf.urls import url, include from app01 import views from app01 import urls as app01_urls from app02 import urls as app02_urls urlpatterns = [ url(r'^test/', views.test), url(r'^blog/', include(app01_urls, namespace="news")), url(r'^blog/', include(app02_urls, namespace="books")), ]
test.html:
這里用的是 namespace_name 格式來獲取 url 路徑
訪問:http://127.0.0.1:8000/test/
點擊“新聞”
跳到了:http://127.0.0.1:8000/blog/blog/,返回的是 /news/blog.html 頁面
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
