您好,登錄后才能下訂單哦!
本章我們將對視圖和URL配置使用一些高超的小技巧。
【流線型化函數導入(streamlining)】
方法一:傳統方法
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), )
方法二:導入views函數,維護較簡單,針對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 ), )
方法三:導入模塊名和視圖函數名,注意用引號括起來
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' ), )
方法四:提取公共視圖前綴,不要再前綴后面和視圖字符串前面放點號,django會自動處理
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沒有一個公共前綴時呢?如下
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'), )
解決:整個框架關注的是urlpatterns模塊級別的變量,patterns返回對象是可相加的。
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調試模式下修改URLconf的行為技術,鏈接debuginfo只在DEBUG配置項為True時才有效。
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函數中位置參數和關鍵字參數的對應關系,其語法 (?P<組名字>匹配模式)。
## 傳統方法 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<year>\d{4})/$', views.year_archive), (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', views.month_archive), )
為了區分它們的區別,以請求/archive/2016/12為例,函數都進行了怎樣的調用?
前者:month_archive(request, '2016', '12')
后者:month_archive(request, year='2016', month='12')
缺點:雖然這樣可讀性強了,而且更準確了,但是冗余性也變差了;而且一個URLconf模式中不允許同時存在命名組和非命名組格式,優先順序:命名組>非命名組>關鍵字傳遞額外參數。
【傳遞額外參數信息】
在我們寫的視圖函數中,我們會發現有好多視圖函數類似,但又有不同,我們怎么樣才能將它寫的更簡潔,原始模板如下:
# 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 判斷,缺點還是把url耦合到代碼里了,更改url的話還得去改視圖函數。
# 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中包含第三個位置參數:關鍵字參數
# 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})
舉例:我們要訪問如下規則的url
/mydata/jan/01/
/mydata/jan/02/
/mydata/jan/03/
# ...
/mydata/dec/30/
/mydata/dec/31/
我們可以設置URLconf 和 視圖函數如下:
urlpatterns = patterns('',
(r'^mydata/(?P<month>\w{3})/(?P<day>\d\d)/$', views.my_view),
)
def my_view(request, month, day):
# ....
但當如果我們想增加訪問一個/mydata/birthday/的url,正常我們要給他一個視圖函數,但我們可以用上面傳遞額外參數解決
urlpatterns = patterns('',
(r'^mydata/birthday/$', views.my_view, {'month': 'jan', 'day': '06'}),
(r'^mydata/(?P<month>\w{3})/(?P<day>\d\d)/$', views.my_view),
)
【include其他URLconf】
有時我們希望我們的代碼用于多個django站點,于是我們就要考慮將我們的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沒有$符,但包含有/,他的意思是當django遇到include,它將截斷匹配的URL,把剩余的字符串發往包含的URLconf進一步處理。
比如我們訪問/weblog/2007/ weblog被此URLconf匹配,/ 截斷的2007就交給了包含的URLconf中的urls。
1、捕獲的參數與include
# root urls.py from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^(?P<username>\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,進而傳遞給那個URLconf中每一個視圖函數。
2、額外的URLconf與include
就像上邊提到的,URLconf有一個第三位置的參數,用字典表示,即下面兩個配置時等效的:
# 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}), )
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。