通用視圖
1. 前言
回想一下,在Django中view層起到的作用是相當于controller的角色,在view中實施的
動作,一般是取得請求參數(shù),再從model中得到數(shù)據(jù),再通過數(shù)據(jù)創(chuàng)建模板,返回相應
響應對象。但在一些比較通用的功能中,比如顯示對象列表,顯示某對象信息,如果反復
寫這么多流程的代碼,也是一件浪費時間的事,在這里,Django同樣給我們提供了類似的
"shortcut"捷徑--通用視圖。
2. 使用通用視圖
使用通用視圖的方法就是在urls.py這個路徑配置文件中進行,創(chuàng)建字典配置信息,然后
傳入patterns里的元組的第三個參數(shù)(extra-parameter),下面來看一個簡單的例子:
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'}), )
運行結果:
可以看到,沒有view的代碼,也可以直接運行。在這里direct_to_template,這個方法
,傳入第三個參數(shù),然后直接進行渲染。
?
?
同時因為direct_to_template是一個函數(shù),我們又可以把它放在view中,下面把
上面的例子改成匹配about/*,任意子網頁。
#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()
運行結果:
安全問題的題外話
上面的例子中,有一個潛在的安全問題,比較容易被忽略。那就是template="about/%s.html" % page這
句,這樣構造路徑容易被名為directory traversal的手段攻擊,簡單的說就是利用"../"這樣的返回父目錄的
路徑操作,去訪問原本不應該被訪問到的服務器上的文件,又被稱為dot dot slash攻擊。比如
使用"http://www.cnblogs.com/../etc/passwd"路徑的話,有可能就能讀取到服務器上的passwd這個文件,從而獲取到
關鍵密碼。
?
發(fā)布這篇博文的時候,cnblogs會把連續(xù)的"../"轉義成"http://www.cnblogs.com",難道是在防止
dot dot slash攻擊?不信,你可以試試。
?
那在上面的例子中會不會有這個問題呢?
答案:不會的。。。
因為\w只會匹配數(shù)字,字母和下劃線,不會去匹配dot這個符號。所以可以安心使用。
回車后,會直接退回到主頁,無法匹配。
3. 用于顯示對象內容的通用視圖
同樣,我們可以只需要model,urls.py文件就可以顯示對象的信息:
#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.htmlPublishers
-
{% for publisher in object_list %}
- {{ publisher.name }} {% endfor %}
也是要構造一個字典參數(shù),包含數(shù)據(jù)源和模板信息,再傳給list_detail.object_list方法,
然后直接完成渲染的工作。運行結果:
4. 通用視圖的幾種擴展用法
4.1 自定義結果集的模板名
上面的例子中 ,模板文件中的變量名一直是object_list,如果有多個數(shù)據(jù)需要顯示,那就
會,通用視圖提供了一種解決這種沖突的命名方法,就是在字典類型中加入template_object_name
變量,此變量+_list就組成模板文件中使用的變量名
publisher_info = { 'queryset': Publisher.objects.all(), 'template_name': 'publisher_list_page.html', 'template_object_name': 'publisher', } 模板文件也要作相應的修改:Publishers
-
{% for publisher in publisher_list %}
- {{ publisher.name }} {% endfor %}
運行結果同上。
4.2 增加額外的context
也是在字典變量中進行修改,增加"extra_context"變量,它的值就是額外的對象數(shù)據(jù)的字典描述,
就可以用在模板文件中使用字典描述中的key來當作變量名。
publisher_info = { 'queryset': Publisher.objects.all(), 'template_object_name': 'publisher', 'template_name': 'publisher_list_page.html', 'extra_context': {'book_list': Book.objects.all()} }
模板文件也要做相應的改:
Publishers
-
{% for publisher in publisher_list %}
- {{ publisher.name }} {% endfor %}
Book
-
{% for book in book_list %}
- {{ book.title }} {% endfor %}
運行結果為:
上面的代碼又有一個問題,那就是'book_list': Book.objects.all(),這段代碼因為
在urls.py中,所以只會在第一次執(zhí)行此路徑的時候執(zhí)行一次,而不會因為Book的值
改變而改變,這是會使用到Django的緩存功能;而"queryset"中的值,Django是不會
緩存的,所以會隨著數(shù)據(jù)改變而改變。
?
解決方法就是使用函數(shù)引用來代替直接的返回值,任何在extra_context中的函數(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 查看結果集的子集
方法很簡單,就是在字典數(shù)據(jù)中使用manage有的方法進行結果集操作,如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 更靈活的結果集操作
上面的代碼可以看到,需要把publisher的名字硬編碼在urls.py文件中,如何才能處理
從用戶傳遞過來的任何publisher名字呢?
答案就是把list_detail.object_list方法放在views.py中調用,就可以使用從request傳遞過來
的參數(shù)。因為list_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對象,而是
賦值給response變量,再進行一些額外的處理,最后再返回HttpResponse對象,這樣就可以
在使用通用視圖功能之前或者之后做一些處理操作。下面例子的功能是在每一次訪問作者之后
,都會更新作者的最后被訪問時間。
#urls.py from mysite.books.views import author_detail urlpatterns = patterns('', # ... url(r'^authors/(?P\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對象 response = list_detail.object_list( request, queryset = Author.objects.all(), object_id = author_id, ) # 記錄訪問該作者的時間 now = datetime.datetime.now() Author.objects.filter(id=author_id).update(last_accessed=now) # 返回通用視圖生成的HttpResponse對象 return response
我們還可以修改HttpResponse對象的相關參數(shù)來達到改變響應信息的目的。比如
def author_list_plaintext(request): response = list_detail.object_list( request, queryset = Author.objects.all(), mimetype = 'text/plain', template_name = 'author_list.txt' ) #修改響應格式,使其的內容不直接顯示在網頁中,而是儲存在文件中,下載下來。 response["Content-Disposition"] = "attachment; filename=authors.txt" return response
模板文件author_list.txt的內容:
Author
-
{% for author in object_list %}
- {{ author.first_name }} {% endfor %}
運行結果為生成authors.txt文件并提供下載:
文本內容會保留HTML標簽信息。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
