django的url使用方法利用django開發站點。能夠設計出很優美的url規則,假設url的匹配規則(包括正規表示式)組織得比較好,view的結構就會比較清晰。比較easy維護。
最簡單的形式
from django.conf.urls import
patterns, url
urlpatterns = patterns(''
, url(r
'^articles/2003/$
', '
news.views.special_case_2003'),
url(r
'^articles/(\d)/$
', '
news.views.year_archive'),
url(r
'^articles/(\d)/(\d)/$
', '
news.views.month_archive'),
url(r
'^articles/(\d)/(\d)/(\d+)/$
', '
news.views.article_detail'),
)當中,正規表示式中組匹配出來的結果能夠作為positional parameters傳遞給view.
假設url是www.yourdomain/articles/2005/,則會匹配第二條規則,執行news.views.year_archive('
2005').
注意點網域名稱部分會被過濾掉
articles的前面不須要加入/,由於前序url的末尾一定會有/不論什麼組匹配的變數,都會議字串的形式傳遞給view, 儘管通過(\d)匹配出了2005,但2005任然會被當做字串傳遞給year_archive
利用named group來傳遞引數
能夠通過下面形式為特定的組指定乙個名稱.
urlpatterns = patterns(''
, url(r
'^articles/2003/$
', '
news.views.special_case_2003'),
url(r
'^articles/(?p\d)/$'
, 'news.views.year_archive'
), url(r'
^articles/(?p\d)/(?p\d)/$
', '
news.views.month_archive'),
url(r
'^articles/(?
p\d)/(?p
\d)/(?p\d)/$'
, 'news.views.article_detail'),)
這種的話,組的匹配結果會通過keyword parameters的形式傳遞給view.比如year_archive(year='
2005')
利用named group能夠為view指定乙個預設引數來匹配多條規則。
#urlconf
from django.conf.urls import
patterns, url
urlpatterns = patterns(''
, url(r
'^blog/$
', '
blog.views.page'),
url(r
'^blog/page(?p\d+)/$
', '
blog.views.page'),
)#view (in blog/views.py)
def page(request, num="1"
):
#指定view字首(提取公因式)
patterns函式的第乙個引數即是view的字首
from django.conf.urls import
patterns, url
urlpatterns = patterns('
news.views',
url(r
'^articles/(\d)/$
', '
year_archive'),
url(r
'^articles/(\d)/(\d)/$
', '
month_archive'),
url(r
'^articles/(\d)/(\d)/(\d+)/$
', '
article_detail'),
)指定多個view字首
urlpatterns = patterns(''
, # 在第乙個引數中直接指定到檢視所在的py檔案中, 當匹配的是方法檢視時,直接寫上方法檢視即可
url(r'^$
', ''),
url(r
'^(?p\d)/(?p
[a-z])/$'
,'month_display'),)
urlpatterns += patterns(
'weblog.views
', # 指定第二個view檔案, 路由到其中的檢視
url(r
'^tag/(?p\w+)/$
', '
tag'),)
include其他匹配模組
from django.conf.urls import
include, patterns, url
urlpatterns = patterns(''
,
#... snip ...
url(r'
^comments/
', include('
django.contrib.comments.urls
')),
url(r
'^community/
', include('
django_website.aggregator.urls
')),
url(r
'^contact/
', include('
django_website.contact.urls
')),
#... snip ...
)當然也能夠直接include其他patterns
from django.conf.urls import
include, patterns, url
extra_patterns = patterns(''
, url(r
'^reports/(?p\d+)/$
', '
credit.views.report'),
url(r
'^charge/$
', '
credit.views.charge'),
)urlpatterns = patterns(''
, url(r'^$
', ''),
url(r
'^help/
', include('
')),
url(r
'^credit/
', include(extra_patterns)),
)為view函式傳遞額外引數
from django.conf.urls import
patterns, url
urlpatterns = patterns('
blog.views',
url(r
'^blog/(?p\d)/$
', '
year_archive
', ),
)直接使用view函式
from django.conf.urls import
patterns, url
from mysite.views import
archive, about, contact
urlpatterns = patterns(''
, url(r
'^archive/$
', archive),
url(r
'^about/$
', about),
url(r
'^contact/$
', contact),
)
django url路由引數錯誤
出現錯誤 typeerror get got an unexpected keyword argument teacher id 出錯原因 view類中,get方法獲得了乙個多餘的額引數,這個引數是通過url正規表示式模版取出的資料,url傳遞過來的引數 如何解決?get方法中再次增加乙個引數去接收...
03 Django URL路由系統
目錄正規表示式 include url分發 命名url 別名 和url的反向解析 命名空間模式 url配置 urlconf 就像django 所支撐 的目錄。它的本質是url與要為該url呼叫的檢視函式之間的對映表。你就是以這種方式告訴django,對於這個url呼叫這段 對於那個url呼叫那段 f...
Linux作業系統靜態路由設定技巧介紹
現在有五個裝置,pc1接rout1,rout1再接rout2,rout2再接rout3,rout3再接pc2,拓撲圖見下 pc1 rout1 rout2 rout3 pc2 pc1 192.168.1.88 24 rout1 192.168.1.128 24 192.168.2.128 24 rou...