詳解Python的Django架構中的通用視圖

來源:互聯網
上載者:User
通用視圖
1. 前言

回想一下,在Django中view層起到的作用是相當於controller的角色,在view中實施的
動作,一般是取得請求參數,再從model中得到資料,再通過資料建立模板,返回相應
響應對象。但在一些比較通用的功能中,比如顯示對象列表,顯示某對象資訊,如果反覆
寫這麼多流程的代碼,也是一件浪費時間的事,在這裡,Django同樣給我們提供了類似的
"shortcut"捷徑--通用視圖。
2. 使用通用視圖
使用通用視圖的方法就是在urls.py這個路徑設定檔中進行,建立字典配置資訊,然後
傳入patterns裡的元組的第三個參數(extra-parameter),下面來看一個簡單的例子:

from django.conf.urls.defaults import *from django.views.generic.simple import direct_to_templateurlpatterns = patterns('',  url(r'^about/$', direct_to_template, {'template': 'about.html'}),)

運行結果:


可以看到,沒有view的代碼,也可以直接運行。在這裡direct_to_template,這個方法
,傳入第三個參數,然後直接進行渲染。


同時因為direct_to_template是一個函數,我們又可以把它放在view中,下面把
上面的例子改成匹配about/*,任意子網頁。

#urls.pyfrom django.conf.urls.defaults import *from django.views.generic.simple import direct_to_templatefrom mysite.books.views import about_pagesurlpatterns = patterns('',  (r'^about/$', direct_to_template, {                    'template': 'about.html'                   }),  (r'^about/(\w+)/$', about_pages),)# view.pyfrom django.http import Http404from django.template import TemplateDoesNotExistfrom django.views.generic.simple import direct_to_template#由正則匹配的參數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這個檔案,從而擷取到
關鍵密碼。

發布這篇博文的時候,cnblogs會把連續的"../"轉義成"http://www.cnblogs.com",難道是在防止
dot dot slash攻擊?不信,你可以試試。

那在上面的例子中會不會有這個問題呢?
答案:不會的。。。
因為\w只會匹配數字,字母和底線,不會去匹配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.pyfrom django.conf.urls.defaults import *from django.views.generic import list_detailfrom mysite.books.models import Publisherpublisher_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

Publishers

    {% for publisher in object_list %}
  • {{ publisher.name }}
  • {% endfor %}

也是要構造一個字典參數,包含資料來源和模板資訊,再傳給list_detail.object_list方法,
然後直接完成渲染的工作。運行結果:

4. 通用視圖的幾種擴充用法
4.1 自訂結果集的模板名
上面的例子中 ,模板檔案中的變數名一直是object_list,如果有多個資料需要顯示,那就
會,通用視圖提供了一種解決這種衝突的命名方法,就是在字典類型中加入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"變數,它的值就是額外的對象資料的字典描述,
就可以用在模板檔案中使用字典描述中的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中,所以只會在第一次執行此路徑的時候執行一次,而不會因為Book的值
改變而改變,這是會使用到Django的緩衝功能;而"queryset"中的值,Django是不會
緩衝的,所以會隨著資料改變而改變。

解決方案就是使用函數引用來代替直接的傳回值,任何在extra_context中的函數都會在
視圖每一次渲染的時候執行一次。所以代碼可以改成:

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},}

只要是引用參數就可以。
4.3 查看結果集的子集
方法很簡單,就是在字典資料中使用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傳遞過來
的參數。因為list_detail.object_list也只不過是普通的python函數。

#urls.pyurlpatterns = patterns('',  url(r'^publishers/$', list_detail.object_list, publisher_info),  url(r'^books/(\w+)/$', books_by_publisher),)#views.pyfrom django.shortcuts import get_object_or_404from django.views.generic import list_detailfrom mysite.books.models import Book, Publisherdef 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的功能,在執行完list_detail.object操作之後,不立即返回HttpResponse對象,而是
賦值給response變數,再進行一些額外的處理,最後再返回HttpResponse對象,這樣就可以
在使用通用視圖功能之前或者之後做一些處理操作。下面例子的功能是在每一次訪問作者之後
,都會更新作者的最後被訪問時間。

#urls.pyfrom mysite.books.views import author_detailurlpatterns = patterns('',  # ...  url(r'^authors/(?P\d+)/$', author_detail),  # ...)#views.pyimport datetimefrom django.shortcuts import get_object_or_404from django.views.generic import list_detailfrom mysite.books.models import Authordef author_detail(request, author_id):  # 執行通用視圖函數,但不立即返回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對象的相關參數來達到改變響應資訊的目的。比如

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標籤資訊。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.