詳解Python的Django框架中的通用視圖
通用視圖
1. 前言
回想一下,在Django中view層起到的作用是相當(dāng)于controller的角色,在view中實(shí)施的
動(dòng)作,一般是取得請(qǐng)求參數(shù),再從model中得到數(shù)據(jù),再通過數(shù)據(jù)創(chuàng)建模板,返回相應(yīng)
響應(yīng)對(duì)象。但在一些比較通用的功能中,比如顯示對(duì)象列表,顯示某對(duì)象信息,如果反復(fù)
寫這么多流程的代碼,也是一件浪費(fèi)時(shí)間的事,在這里,Django同樣給我們提供了類似的
"shortcut"捷徑--通用視圖。
2. 使用通用視圖
使用通用視圖的方法就是在urls.py這個(gè)路徑配置文件中進(jìn)行,創(chuàng)建字典配置信息,然后
傳入patterns里的元組的第三個(gè)參數(shù)(extra-parameter),下面來看一個(gè)簡(jiǎn)單的例子:
from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template urlpatterns = patterns('', url(r'^about/$', direct_to_template, {'template': 'about.html'}), )
運(yùn)行結(jié)果:
可以看到,沒有view的代碼,也可以直接運(yùn)行。在這里direct_to_template,這個(gè)方法
,傳入第三個(gè)參數(shù),然后直接進(jìn)行渲染。
同時(shí)因?yàn)閐irect_to_template是一個(gè)函數(shù),我們又可以把它放在view中,下面把
上面的例子改成匹配about/*,任意子網(wǎng)頁。
#urls.py from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template from mysite.books.views import about_pages urlpatterns = patterns('', (r'^about/$', direct_to_template, { 'template': 'about.html' }), (r'^about/(\w+)/$', about_pages), ) # view.py from django.http import Http404 from django.template import TemplateDoesNotExist from django.views.generic.simple import direct_to_template #由正則匹配的參數(shù) def about_pages(request, page): try: return direct_to_template(request, template="about/%s.html" % page)#返回的HttpResponse except TemplateDoesNotExist: raise Http404()
運(yùn)行結(jié)果:
安全問題的題外話
上面的例子中,有一個(gè)潛在的安全問題,比較容易被忽略。那就是template="about/%s.html" % page這
句,這樣構(gòu)造路徑容易被名為directory traversal的手段攻擊,簡(jiǎn)單的說就是利用"../"這樣的返回父目錄的
路徑操作,去訪問原本不應(yīng)該被訪問到的服務(wù)器上的文件,又被稱為dot dot slash攻擊。比如
使用"http://www.cnblogs.com/../etc/passwd"路徑的話,有可能就能讀取到服務(wù)器上的passwd這個(gè)文件,從而獲取到
關(guān)鍵密碼。
發(fā)布這篇博文的時(shí)候,cnblogs會(huì)把連續(xù)的"../"轉(zhuǎn)義成"http://www.cnblogs.com",難道是在防止
dot dot slash攻擊?不信,你可以試試。
那在上面的例子中會(huì)不會(huì)有這個(gè)問題呢?
答案:不會(huì)的。。。
因?yàn)閈w只會(huì)匹配數(shù)字,字母和下劃線,不會(huì)去匹配dot這個(gè)符號(hào)。所以可以安心使用。
回車后,會(huì)直接退回到主頁,無法匹配。
3. 用于顯示對(duì)象內(nèi)容的通用視圖
同樣,我們可以只需要model,urls.py文件就可以顯示對(duì)象的信息:
#model.py,之前例子中Publisher的定義
class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() def __unicode__(self): return self.name #urls.py from django.conf.urls.defaults import * from django.views.generic import list_detail from mysite.books.models import Publisher publisher_info = { 'queryset': Publisher.objects.all(), 'template_name': 'publisher_list_page.html', } urlpatterns = patterns('', url(r'^publishers/$', list_detail.object_list, publisher_info), ) #publisher_list_page.html <h2>Publishers</h2> <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
也是要構(gòu)造一個(gè)字典參數(shù),包含數(shù)據(jù)源和模板信息,再傳給list_detail.object_list方法,
然后直接完成渲染的工作。運(yùn)行結(jié)果:
4. 通用視圖的幾種擴(kuò)展用法
4.1 自定義結(jié)果集的模板名
上面的例子中 ,模板文件中的變量名一直是object_list,如果有多個(gè)數(shù)據(jù)需要顯示,那就
會(huì),通用視圖提供了一種解決這種沖突的命名方法,就是在字典類型中加入template_object_name
變量,此變量+_list就組成模板文件中使用的變量名
publisher_info = { 'queryset': Publisher.objects.all(), 'template_name': 'publisher_list_page.html', 'template_object_name': 'publisher', } 模板文件也要作相應(yīng)的修改: <h2>Publishers</h2> <ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
運(yùn)行結(jié)果同上。
4.2 增加額外的context
也是在字典變量中進(jìn)行修改,增加"extra_context"變量,它的值就是額外的對(duì)象數(shù)據(jù)的字典描述,
就可以用在模板文件中使用字典描述中的key來當(dāng)作變量名。
publisher_info = { 'queryset': Publisher.objects.all(), 'template_object_name': 'publisher', 'template_name': 'publisher_list_page.html', 'extra_context': {'book_list': Book.objects.all()} }
模板文件也要做相應(yīng)的改:
<h2>Publishers</h2> <ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> <h2>Book</h2> <ul> {% for book in book_list %} <li>{{ book.title }}</li> {% endfor %} </ul>
運(yùn)行結(jié)果為:
上面的代碼又有一個(gè)問題,那就是'book_list': Book.objects.all(),這段代碼因?yàn)?br />
在urls.py中,所以只會(huì)在第一次執(zhí)行此路徑的時(shí)候執(zhí)行一次,而不會(huì)因?yàn)锽ook的值
改變而改變,這是會(huì)使用到Django的緩存功能;而"queryset"中的值,Django是不會(huì)
緩存的,所以會(huì)隨著數(shù)據(jù)改變而改變。
解決方法就是使用函數(shù)引用來代替直接的返回值,任何在extra_context中的函數(shù)都會(huì)在
視圖每一次渲染的時(shí)候執(zhí)行一次。所以代碼可以改成:
def get_books(): return Book.objects.all() publisher_info = { 'queryset': Publisher.objects.all(), 'template_object_name': 'publisher', 'template_name': 'publisher_list_page.html', 'extra_context': {'book_list': get_books}, }
或者改寫成:
publisher_info = { 'queryset': Publisher.objects.all(), 'template_object_name': 'publisher', 'extra_context': {'book_list': Book.objects.all}, }
只要是引用參數(shù)就可以。
4.3 查看結(jié)果集的子集
方法很簡(jiǎn)單,就是在字典數(shù)據(jù)中使用manage有的方法進(jìn)行結(jié)果集操作,如filter等。
apress_books = { 'queryset': Book.objects.filter(publisher__name='Apress'), 'template_name': 'books/apress_list.html', } urlpatterns = patterns('', url(r'^books/apress/$', list_detail.object_list, apress_books), )
4.4 更靈活的結(jié)果集操作
上面的代碼可以看到,需要把publisher的名字硬編碼在urls.py文件中,如何才能處理
從用戶傳遞過來的任何publisher名字呢?
答案就是把list_detail.object_list方法放在views.py中調(diào)用,就可以使用從request傳遞過來
的參數(shù)。因?yàn)閘ist_detail.object_list也只不過是普通的python函數(shù)。
#urls.py urlpatterns = patterns('', url(r'^publishers/$', list_detail.object_list, publisher_info), url(r'^books/(\w+)/$', books_by_publisher), ) #views.py from django.shortcuts import get_object_or_404 from django.views.generic import list_detail from mysite.books.models import Book, Publisher def books_by_publisher(request, name): # Look up the publisher (and raise a 404 if it can't be found). publisher = get_object_or_404(Publisher, name__iexact=name) # Use the object_list view for the heavy lifting. return list_detail.object_list( request, queryset = Book.objects.filter(publisher=publisher), template_name = 'books_by_publisher.html', template_object_name = 'book', extra_context = {'publisher': publisher} )
list_detail.object_list返回的也是HttpResponse,
4.5 利用通用視圖做額外工作
利用4.4的功能,在執(zhí)行完list_detail.object操作之后,不立即返回HttpResponse對(duì)象,而是
賦值給response變量,再進(jìn)行一些額外的處理,最后再返回HttpResponse對(duì)象,這樣就可以
在使用通用視圖功能之前或者之后做一些處理操作。下面例子的功能是在每一次訪問作者之后
,都會(huì)更新作者的最后被訪問時(shí)間。
#urls.py from mysite.books.views import author_detail urlpatterns = patterns('', # ... url(r'^authors/(?P<author_id>\d+)/$', author_detail), # ... ) #views.py import datetime from django.shortcuts import get_object_or_404 from django.views.generic import list_detail from mysite.books.models import Author def author_detail(request, author_id): # 執(zhí)行通用視圖函數(shù),但不立即返回HttpResponse對(duì)象 response = list_detail.object_list( request, queryset = Author.objects.all(), object_id = author_id, ) # 記錄訪問該作者的時(shí)間 now = datetime.datetime.now() Author.objects.filter(id=author_id).update(last_accessed=now) # 返回通用視圖生成的HttpResponse對(duì)象 return response
我們還可以修改HttpResponse對(duì)象的相關(guān)參數(shù)來達(dá)到改變響應(yīng)信息的目的。比如
def author_list_plaintext(request): response = list_detail.object_list( request, queryset = Author.objects.all(), mimetype = 'text/plain', template_name = 'author_list.txt' ) #修改響應(yīng)格式,使其的內(nèi)容不直接顯示在網(wǎng)頁中,而是儲(chǔ)存在文件中,下載下來。 response["Content-Disposition"] = "attachment; filename=authors.txt" return response
模板文件author_list.txt的內(nèi)容:
<h2>Author</h2> <ul> {% for author in object_list %} <li>{{ author.first_name }}</li> {% endfor %} </ul>
運(yùn)行結(jié)果為生成authors.txt文件并提供下載:
文本內(nèi)容會(huì)保留HTML標(biāo)簽信息。
相關(guān)文章
基于OpenCV實(shí)現(xiàn)小型的圖像數(shù)據(jù)庫檢索功能
下面就使用VLAD表示圖像,實(shí)現(xiàn)一個(gè)小型的圖像數(shù)據(jù)庫的檢索程序。下面實(shí)現(xiàn)需要的功能模塊,分步驟給大家介紹的非常詳細(xì),對(duì)OpenCV圖像數(shù)據(jù)庫檢索功能感興趣的朋友跟隨小編一起看看吧2021-12-125個(gè)Python殺手級(jí)的自動(dòng)化腳本分享
Python是一種功能強(qiáng)大的語言,廣泛用于自動(dòng)執(zhí)行各種任務(wù),只要想通過自動(dòng)化日常任務(wù)來節(jié)省時(shí)間的人,Python?都能滿足您的需求。本文整理了5個(gè)Python腳本,希望對(duì)大家有所幫助2023-01-01windows下pycharm搭建spark環(huán)境并成功運(yùn)行 附源碼
這篇文章主要介紹了windows下pycharm搭建spark環(huán)境并成功運(yùn)行 附源碼,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Django Admin 實(shí)現(xiàn)外鍵過濾的方法
下面小編就為大家?guī)硪黄狣jango Admin 實(shí)現(xiàn)外鍵過濾的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09打印tensorflow恢復(fù)模型中所有變量與操作節(jié)點(diǎn)方式
這篇文章主要介紹了打印tensorflow恢復(fù)模型中所有變量與操作節(jié)點(diǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè)
這篇文章主要為大家介紹了PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05