這篇文章將為大家詳細(xì)講解有關(guān)如何在Django中使用url,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián),專注為中小企業(yè)提供官網(wǎng)建設(shè)、營銷型網(wǎng)站制作、響應(yīng)式網(wǎng)站開發(fā)、展示型成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站等服務(wù),幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價(jià)值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營銷推廣問題。最簡單的形式
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^articles/2003/$', 'news.views.special_case_2003'),
url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
其中,正則表達(dá)式中組匹配出來的結(jié)果可以作為positional parameters傳遞給view.
如果url是www.yourdomain/articles/2005/,則會(huì)匹配第二條規(guī)則,執(zhí)行news.views.year_archive('2005').
注意點(diǎn)
域名部分會(huì)被過濾掉
articles的前面不需要添加/,因?yàn)榍靶騯rl的末尾一定會(huì)有/
任何組匹配的變量,都會(huì)議字符串的形式傳遞給view, 雖然通過(\d{4})匹配出了2005,但2005任然會(huì)被當(dāng)做字符串傳遞給year_archive
利用named group來傳遞參數(shù)
可以通過以下形式為特定的組指定一個(gè)名稱.
urlpatterns = patterns('', url(r'^articles/2003/$', 'news.views.special_case_2003'), url(r'^articles/(?P\d{4})/$', 'news.views.year_archive'), url(r'^articles/(?P \d{4})/(?P \d{2})/$', 'news.views.month_archive'), url(r'^articles/(?P \d{4})/(?P \d{2})/(?P \d{2})/$', 'news.views.article_detail'), )
這樣的話,組的匹配結(jié)果會(huì)通過keyword parameters的形式傳遞給view.例如year_archive(year='2005')
利用named group可以為view指定一個(gè)默認(rèn)參數(shù)來匹配多條規(guī)則。
# 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"): # Output the appropriate page of blog entries, according to num.
指定view前綴(提取公因式)
patterns函數(shù)的第一個(gè)參數(shù)即是view的前綴
from django.conf.urls import patterns, url urlpatterns = patterns('news.views', url(r'^articles/(\d{4})/$', 'year_archive'), url(r'^articles/(\d{4})/(\d{2})/$', 'month_archive'), url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'), )
指定多個(gè)view前綴
urlpatterns = patterns('myapp.views', url(r'^$', 'app_index'), url(r'^(?P\d{4})/(?P [a-z]{3})/$','month_display'), ) urlpatterns += patterns('weblog.views', 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 ... )
當(dāng)然也可以直接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'^$', 'apps.main.views.homepage'), url(r'^help/', include('apps.help.urls')), url(r'^credit/', include(extra_patterns)), )
為view函數(shù)傳遞額外參數(shù)
from django.conf.urls import patterns, url urlpatterns = patterns('blog.views', url(r'^blog/(?P\d{4})/$', 'year_archive', {'foo': 'bar'}), )
直接使用view函數(shù)
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), )
關(guān)于如何在Django中使用url就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。