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,訪問(wèn)的話就不知道該訪問(wèn)哪一個(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 格式來(lái)獲取 url 路徑
訪問(wèn):http://127.0.0.1:8000/test/
點(diǎn)擊“新聞”
跳到了:http://127.0.0.1:8000/blog/blog/,返回的是 /news/blog.html 頁(yè)面
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
linux環(huán)境下的python安裝過(guò)程圖解(含setuptools)
這篇文章主要介紹了linux環(huán)境下的python安裝過(guò)程圖解(含setuptools),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11python如何實(shí)現(xiàn)excel數(shù)據(jù)添加到mongodb
本文介紹了python是如何實(shí)現(xiàn)excel數(shù)據(jù)添加到mongodb,為了將數(shù)據(jù)導(dǎo)入mongodb,引入了pymongo,xlrd包,需要的朋友可以參考下2015-07-07Python xlrd excel文件操作代碼實(shí)例
這篇文章主要介紹了Python xlrd excel文件操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python中raise用法簡(jiǎn)單實(shí)例(超級(jí)詳細(xì),看了無(wú)師自通)
python中raise語(yǔ)句用于手動(dòng)觸發(fā)異常,通過(guò)raise語(yǔ)句可以在代碼中顯式地引發(fā)異常,從而使程序進(jìn)入異常處理流程,下面這篇文章主要給大家介紹了關(guān)于Python中raise用法的相關(guān)資料,需要的朋友可以參考下2024-03-03深入理解Python虛擬機(jī)中描述器的實(shí)現(xiàn)原理
這篇文章主要給大家介紹一個(gè)我們?cè)谑褂妙惖臅r(shí)候經(jīng)常使用但是卻很少在意的黑科技——描述器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05python3實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Python小程序 控制鼠標(biāo)循環(huán)點(diǎn)擊代碼實(shí)例
這篇文章主要介紹了Python小程序 控制鼠標(biāo)循環(huán)點(diǎn)擊代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10python用BeautifulSoup庫(kù)簡(jiǎn)單爬蟲實(shí)例分析
文章給大家分享了關(guān)于python爬蟲的相關(guān)實(shí)例以及相關(guān)代碼,有興趣的朋友們參考下。2018-07-07