欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python Django 命名空間模式的實(shí)現(xiàn)

 更新時(shí)間:2019年08月09日 10:47:19   作者:Sch01aR#  
這篇文章主要介紹了Python Django 命名空間模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

新建一個(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)文章

最新評(píng)論