詳解Python的Django框架中的通用視圖
通用視圖
1. 前言
回想一下,在Django中view層起到的作用是相當(dāng)于controller的角色,在view中實(shí)施的
動(dòng)作,一般是取得請求參數(shù),再從model中得到數(shù)據(jù),再通過數(shù)據(jù)創(chuàng)建模板,返回相應(yīng)
響應(yīng)對象。但在一些比較通用的功能中,比如顯示對象列表,顯示某對象信息,如果反復(fù)
寫這么多流程的代碼,也是一件浪費(fèi)時(shí)間的事,在這里,Django同樣給我們提供了類似的
"shortcut"捷徑--通用視圖。
2. 使用通用視圖
使用通用視圖的方法就是在urls.py這個(gè)路徑配置文件中進(jìn)行,創(chuàng)建字典配置信息,然后
傳入patterns里的元組的第三個(gè)參數(shù)(extra-parameter),下面來看一個(gè)簡單的例子:
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的手段攻擊,簡單的說就是利用"../"這樣的返回父目錄的
路徑操作,去訪問原本不應(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è)符號。所以可以安心使用。

回車后,會(huì)直接退回到主頁,無法匹配。
3. 用于顯示對象內(nèi)容的通用視圖
同樣,我們可以只需要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.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"變量,它的值就是額外的對象數(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é)果集的子集
方法很簡單,就是在字典數(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對象,而是
賦值給response變量,再進(jìn)行一些額外的處理,最后再返回HttpResponse對象,這樣就可以
在使用通用視圖功能之前或者之后做一些處理操作。下面例子的功能是在每一次訪問作者之后
,都會(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對象
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對象
return response
我們還可以修改HttpResponse對象的相關(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)文章
Python實(shí)戰(zhàn)爬蟲之女友欲買文胸不知何色更美
實(shí)踐來源于理論,做爬蟲前肯定要先了解相關(guān)的規(guī)則和原理,網(wǎng)絡(luò)爬蟲又稱為網(wǎng)頁蜘蛛,網(wǎng)絡(luò)機(jī)器人,更經(jīng)常的稱為網(wǎng)頁追逐者,是一種按照一定的規(guī)則,自動(dòng)地抓取萬維網(wǎng)信息的程序或者腳本。一句話概括就是網(wǎng)上信息搬運(yùn)工。本篇文章帶你深入了解,需要的朋友可以參考下2021-09-09
Python摸魚神器之利用樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面
這篇文章主要介紹了Python摸魚神器樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
pandas實(shí)現(xiàn)excel表格處理并讀取指定sheet的方法
這篇文章主要介紹了pandas實(shí)現(xiàn)excel表格處理并讀取指定sheet的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
python 獲取一個(gè)值在某個(gè)區(qū)間的指定倍數(shù)的值方法
今天小編就為大家分享一篇python 獲取一個(gè)值在某個(gè)區(qū)間的指定倍數(shù)的值方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
python實(shí)現(xiàn)數(shù)組平移K位問題
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)組平移K位問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

