詳解Django通用視圖中的函數封裝

來源:互聯網
上載者:User
用函數封裝來處理複雜的資料過濾

另一個常見的需求是按URL裡的關鍵字來過濾資料對象。 之前,我們在URLconf中寫入程式碼了出版商的名字,但是如果我們想用一個視圖就顯示某個任意指定的出版商的所有書籍,那該怎麼辦呢? 我們可以通過對 object_list 通用視圖進行封裝來避免 寫一大堆的手工代碼。 按慣例,我們先從寫URL配置開始:

urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info), **(r'^books/(\w+)/$', books_by_publisher),**)

接下來,我們寫 books_by_publisher 這個視圖:

from 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/books_by_publisher.html',  template_object_name = 'book',  extra_context = {'publisher': publisher} )

這樣寫沒問題,因為通用視圖就是Python函數。 和其他的視圖函數一樣,通用視圖也是接受一些 參數並返回 HttpResponse 對象。 因此,通過封裝通用視圖函數可以做更多的事。

注意

注意在前面這個例子中我們在 extra_context中傳遞了當前出版商這個參數。
處理額外工作

我們再來看看最後一個常用模式:

想象一下我們在 Author 對象裡有一個 last_accessed 欄位,我們用這個欄位來記錄最近一次對author的訪問。 當然通用視圖 object_detail 並不能處理這個問題,但是我們仍然可以很容易地編寫一個自訂的視圖來更新這個欄位。

首先,我們需要在URL配置裡設定指向到新的自訂視圖:

from mysite.books.views import author_detailurlpatterns = patterns('', # ... **(r'^authors/(?P\d+)/$', author_detail),** # ...)

接下來寫封裝函數:

import datetimefrom django.shortcuts import get_object_or_404from django.views.generic import list_detailfrom mysite.books.models import Authordef author_detail(request, author_id): # Delegate to the generic view and get an HttpResponse. response = list_detail.object_detail(  request,  queryset = Author.objects.all(),  object_id = author_id, ) # Record the last accessed date. We do this *after* the call # to object_detail(), not before it, so that this won't be called # unless the Author actually exists. (If the author doesn't exist, # object_detail() will raise Http404, and we won't reach this point.) now = datetime.datetime.now() Author.objects.filter(id=author_id).update(last_accessed=now) return response

注意

除非你添加 last_accessed 欄位到你的 Author 模型並建立 books/author_detail.html 模板,否則這段代碼不能真正工作。

我們可以用同樣的方法修改通用視圖的傳回值。 如果我們想要提供一個供下載用的 純文字版本的author列表,我們可以用下面這個視圖:

def author_list_plaintext(request): response = list_detail.object_list(  request,  queryset = Author.objects.all(),  mimetype = 'text/plain',  template_name = 'books/author_list.txt' ) response["Content-Disposition"] = "attachment; filename=authors.txt" return response

這個方法之所以工作是因為通用視圖返回的 HttpResponse 對象可以象一個字典 一樣的設定HTTP的頭部。 隨便說一下,這個 Content-Disposition 的含義是 告訴瀏覽器下載並儲存這個頁面,而不是在瀏覽器中顯示它。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.