本章我們將對(duì)視圖和URL配置使用一些高超的小技巧。
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到通河網(wǎng)站設(shè)計(jì)與通河網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋通河地區(qū)。【流線(xiàn)型化函數(shù)導(dǎo)入(streamlining)】
方法一:傳統(tǒng)方法
from django.conf.urls.defaults import * from mysite.views import hello, current_datetime, hours_ahead urlpatterns = patterns('', (r'^hello/$', hello), (r'^time/$', current_datetime), (r'^time/plus/(\d{1,2})/$', hours_ahead), )
方法二:導(dǎo)入views函數(shù),維護(hù)較簡(jiǎn)單,針對(duì)import的視圖模塊
from django.conf.urls.defaults import * **from mysite import views** urlpatterns = patterns('', (r'^hello/$', views.hello'), (r'^time/$', views.current_datetime ), (r'^time/plus/(d{1,2})/$', views.hours_ahead ), )
方法三:導(dǎo)入模塊名和視圖函數(shù)名,注意用引號(hào)括起來(lái)
from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^hello/$', 'mysite.views.hello' ), (r'^time/$', 'mysite.views.current_datetime' ), (r'^time/plus/(d{1,2})/$', 'mysite.views.hours_ahead' ), )
方法四:提取公共視圖前綴,不要再前綴后面和視圖字符串前面放點(diǎn)號(hào),django會(huì)自動(dòng)處理
from django.conf.urls.defaults import * urlpatterns = patterns('mysite.views' , (r'^hello/$', 'hello' ), (r'^time/$', 'current_datetime' ), (r'^time/plus/(d{1,2})/$', 'hours_ahead' ), )
方法四(2):如果我們URLconf沒(méi)有一個(gè)公共前綴時(shí)呢?如下
from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^hello/$', 'mysite.views.hello'), (r'^time/$', 'mysite.views.current_datetime'), (r'^time/plus/(\d{1,2})/$', 'mysite.views.hours_ahead'), (r'^tag/(\w+)/$', 'weblog.views.tag'), )
解決:整個(gè)框架關(guān)注的是urlpatterns模塊級(jí)別的變量,patterns返回對(duì)象是可相加的。
from django.conf.urls.defaults import * urlpatterns = patterns('mysite.views', (r'^hello/$', 'hello'), (r'^time/$', 'current_datetime'), (r'^time/plus/(\d{1,2})/$', 'hours_ahead'), ) urlpatterns += patterns('weblog.views', (r'^tag/(\w+)/$', 'tag'), )
特例:django調(diào)試模式下修改URLconf的行為技術(shù),鏈接debuginfo只在DEBUG配置項(xiàng)為T(mén)rue時(shí)才有效。
from django.conf import settings from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', ..... ) if settings.DEBUG: urlpatterns += patterns('', (r'^debuginfo/$', views.debug), )
【url的命名法匹配模式】
方法一:使用命名組,好比python函數(shù)中位置參數(shù)和關(guān)鍵字參數(shù)的對(duì)應(yīng)關(guān)系,其語(yǔ)法 (?P<組名字>匹配模式)。
## 傳統(tǒng)方法 from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^articles/(\d{4})/$', views.year_archive), (r'^articles/(\d{4})/(\d{2})/$', views.month_archive), ) ## 使用命名組 from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^articles/(?P\d{4})/$', views.year_archive), (r'^articles/(?P \d{4})/(?P \d{2})/$', views.month_archive), )
為了區(qū)分它們的區(qū)別,以請(qǐng)求/archive/2016/12為例,函數(shù)都進(jìn)行了怎樣的調(diào)用?
前者:month_archive(request, '2016', '12')
后者:month_archive(request, year='2016', month='12')
缺點(diǎn):雖然這樣可讀性強(qiáng)了,而且更準(zhǔn)確了,但是冗余性也變差了;而且一個(gè)URLconf模式中不允許同時(shí)存在命名組和非命名組格式,優(yōu)先順序:命名組>非命名組>關(guān)鍵字傳遞額外參數(shù)。
【傳遞額外參數(shù)信息】
在我們寫(xiě)的視圖函數(shù)中,我們會(huì)發(fā)現(xiàn)有好多視圖函數(shù)類(lèi)似,但又有不同,我們?cè)趺礃硬拍軐⑺鼘?xiě)的更簡(jiǎn)潔,原始模板如下:
# urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^foo/$', views.foo_view), (r'^bar/$', views.bar_view), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foo_view(request): m_list = MyModel.objects.filter(is_new=True) return render_to_response('template1.html', {'m_list': m_list}) def bar_view(request): m_list = MyModel.objects.filter(is_new=True) return render_to_response('template2.html', {'m_list': m_list})
方法一:添加if 判斷,缺點(diǎn)還是把url耦合到代碼里了,更改url的話(huà)還得去改視圖函數(shù)。
# views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, url): m_list = MyModel.objects.filter(is_new=True) if url == 'foo': template_name = 'template1.html' elif url == 'bar': template_name = 'template2.html' return render_to_response(template_name, {'m_list': m_list})
方法二:URLconf中包含第三個(gè)位置參數(shù):關(guān)鍵字參數(shù)
# urls.py from django.conf.urls.defaults import * from mysite import viewsurlpatterns = patterns('', (r'^foo/$', views.foobar_view, {'template_name': 'template1.html'}), (r'^bar/$', views.foobar_view, {'template_name': 'template2.html'}), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, {'m_list': m_list})
舉例:我們要訪(fǎng)問(wèn)如下規(guī)則的url
/mydata/jan/01/
/mydata/jan/02/
/mydata/jan/03/
# ...
/mydata/dec/30/
/mydata/dec/31/
我們可以設(shè)置URLconf 和 視圖函數(shù)如下:
urlpatterns = patterns('',
(r'^mydata/(?P
)
def my_view(request, month, day):
# ....
但當(dāng)如果我們想增加訪(fǎng)問(wèn)一個(gè)/mydata/birthday/的url,正常我們要給他一個(gè)視圖函數(shù),但我們可以用上面?zhèn)鬟f額外參數(shù)解決
urlpatterns = patterns('',
(r'^mydata/birthday/$', views.my_view, {'month': 'jan', 'day': '06'}),
(r'^mydata/(?P
)
【include其他URLconf】
有時(shí)我們希望我們的代碼用于多個(gè)django站點(diǎn),于是我們就要考慮將我們的URLconf以包含的方式處理。
from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^weblog/', include('mysite.blog.urls')), (r'^photos/', include('mysite.photos.urls')), (r'^about/$', 'mysite.views.about'), )
注:在包含其他urls的url沒(méi)有$符,但包含有/,他的意思是當(dāng)django遇到include,它將截?cái)嗥ヅ涞腢RL,把剩余的字符串發(fā)往包含的URLconf進(jìn)一步處理。
比如我們?cè)L問(wèn)/weblog/2007/ weblog被此URLconf匹配,/ 截?cái)嗟?007就交給了包含的URLconf中的urls。
1、捕獲的參數(shù)與include
# root urls.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^(?P\w+)/blog/', include('foo.urls.blog')), ) # foo/urls/blog.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^$', 'foo.views.blog_index'), (r'^archive/$', 'foo.views.blog_archive'), )
本例中,被捕獲的username變量將傳遞給被包含的URLconf,進(jìn)而傳遞給那個(gè)URLconf中每一個(gè)視圖函數(shù)。
2、額外的URLconf與include
就像上邊提到的,URLconf有一個(gè)第三位置的參數(shù),用字典表示,即下面兩個(gè)配置時(shí)等效的:
# urls.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^blog/', include('inner'), {'blogid': 3}), ) # inner.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^archive/$', 'mysite.views.archive'), (r'^about/$', 'mysite.views.about'), (r'^rss/$', 'mysite.views.rss'), )
# urls.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^blog/', include('inner')), ) # inner.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^archive/$', 'mysite.views.archive', {'blogid': 3}), (r'^about/$', 'mysite.views.about', {'blogid': 3}), (r'^rss/$', 'mysite.views.rss', {'blogid': 3}), )
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。