Django之URL的命名空間和命名模式

2021-08-08 02:44:52 字數 1378 閱讀 9539

django之url的命名空間和命名模式

url%} template tag?

polls/urls.py

from

django.conf.urls

import

urlfrom

.import

views

='polls'

urlpatterns=[

url(

r'^$'

,views

.index

,name

='index'

),url(r

'^(?p[0-9]+)/$'

,views

.detail

,name

='detail'

),url(r

'^(?p[0-9]+)/results/$'

,views

.results

,name

='results'

),url(r

'^(?p[0-9]+)/vote/$'

,views

.vote

,name

='vote'),]

now change yourpolls/index.htmltemplate from:

polls/templates/polls/index.html

<

li><

ahref="

">}a

>

li>

to point at the namespaced detail view:

polls/templates/polls/index.html

<

li><

ahref="

">}a

>

li>

url命名空間是用於模板檔案中對url(所謂url是瀏覽器訪問時的網路位址)的反向引用,一般會使用模板標籤

例如在index.html中

"

例如在views.py中

或者index.html中

<

li><

ahref

="/polls/

***.html

">}a

>

li>

Django之命名空間(namespace)

命名空間 namespace 用來表示識別符號的作用範圍 乙個識別符號可以在多個命名空間中定義,它在不同命名空間中的含義是互不相干的。這樣,在乙個新的命名空間中可定義任何識別符號,它們不會與任何已有的識別符號發生衝突。5 啟動django,瀏覽器訪問url結果。顯然,這並不是我們想要的結果。原因 由...

django 路由控制之命名空間

命名空間 英語 namespace 是表示識別符號的可見範圍。乙個識別符號可在多個命名空間中定義,它在不同命名空間中的含義是互不相干的。這樣,在乙個新的命名空間中可定義任何識別符號,它們不會與任何已有的識別符號發生衝突,因為已有的定義都處於其它命名空間中。由於name沒有作用域,django在反解u...

Django命名URL和反向解析URL實現解析

命名 url test.html 測試頁面 測試頁面 json test relbinbohwe external nofollow json 資料 urls.py from django.conf.urls import uwww.cppcns.comrl from app01 import vi...