Python Django 命名空間模式的實(shí)現(xiàn)
新建一個(gè)項(xiàng)目 app02

在 app02/ 下創(chuàng)建 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"), ]
這兩個(gè)都有 blog/ 路徑,且都名為 blog,訪問的話就不知道該訪問哪一個(gè)
這時(shí)候需要用到命名空間
在 templates 目錄下創(chuàng)建 /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:
<a href="{% url 'books:blog' %}" rel="external nofollow" >書籍</a>
<a href="{% url 'news:blog' %}" rel="external nofollow" >新聞</a>
這里用的是 namespace_name 格式來獲取 url 路徑
訪問:http://127.0.0.1:8000/test/

點(diǎn)擊“新聞”

跳到了:http://127.0.0.1:8000/blog/blog/,返回的是 /news/blog.html 頁面
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作
這篇文章主要介紹了pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
使用Python實(shí)現(xiàn)操作mongodb詳解
這篇文章主要為大家詳細(xì)介紹了使用Python實(shí)現(xiàn)操作mongodb的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
Python插件virtualenv搭建虛擬環(huán)境
這篇文章主要為大家詳細(xì)介紹了Python插件virtualenv搭建虛擬環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
最新PyCharm 2021.3.1永久激活碼(親測(cè)有效)
今天又有朋友反應(yīng)PyCharm2021提示激活碼過期了,下面再為大家分享一個(gè)2022年01月08日更新PyCharm2021最新激活碼,需要的朋友可以參考下2020-11-11
python實(shí)現(xiàn)嵌套列表平鋪的兩種方法
今天小編就為大家分享一篇python實(shí)現(xiàn)嵌套列表平鋪的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python接口自動(dòng)化之request請(qǐng)求封裝源碼分析
這篇文章主要介紹了Python接口自動(dòng)化之request請(qǐng)求封裝源碼分析,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06

