国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-01-04 18:00:41
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了詳解Django通用視圖中的函數包裝,Django是最具人氣的Python web開發框架,需要的朋友可以參考下
用函數包裝來處理復雜的數據過濾
另一個常見的需求是按URL里的關鍵字來過濾數據對象。 之前,我們在URLconf中硬編碼了出版商的名字,但是如果我們想用一個視圖就顯示某個任意指定的出版商的所有書籍,那該怎么辦呢? 我們可以通過對 object_list 通用視圖進行包裝來避免 寫一大堆的手工代碼。 按慣例,我們先從寫URL配置開始:
 

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


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

  1. from django.shortcuts import get_object_or_404 
  2. from django.views.generic import list_detail 
  3. from mysite.books.models import Book, Publisher 
  4.  
  5. def books_by_publisher(request, name): 
  6.  
  7. # Look up the publisher (and raise a 404 if it can't be found). 
  8. publisher = get_object_or_404(Publisher, name__iexact=name) 
  9.  
  10. # Use the object_list view for the heavy lifting. 
  11. return list_detail.object_list( 
  12.   request, 
  13.   queryset = Book.objects.filter(publisher=publisher), 
  14.   template_name = 'books/books_by_publisher.html'
  15.   template_object_name = 'book'
  16.   extra_context = {'publisher': publisher} 


這樣寫沒問題,因為通用視圖就是Python函數。 和其他的視圖函數一樣,通用視圖也是接受一些 參數并返回 HttpResponse 對象。 因此,通過包裝通用視圖函數可以做更多的事。
注意
注意在前面這個例子中我們在 extra_context中傳遞了當前出版商這個參數。
處理額外工作
我們再來看看最后一個常用模式:
想象一下我們在 Author 對象里有一個 last_accessed 字段,我們用這個字段來記錄最近一次對author的訪問。 當然通用視圖 object_detail 并不能處理這個問題,但是我們仍然可以很容易地編寫一個自定義的視圖來更新這個字段。
首先,我們需要在URL配置里設置指向到新的自定義視圖:
 

  1. from mysite.books.views import author_detail 
  2.  
  3. urlpatterns = patterns(''
  4. # ... 
  5. **(r'^authors/(?P<author_id>/d+)/$', author_detail),** 
  6. # ... 



接下來寫包裝函數:
 

  1. import datetime 
  2. from django.shortcuts import get_object_or_404 
  3. from django.views.generic import list_detail 
  4. from mysite.books.models import Author 
  5.  
  6. def author_detail(request, author_id): 
  7. # Delegate to the generic view and get an HttpResponse. 
  8. response = list_detail.object_detail( 
  9.   request, 
  10.   queryset = Author.objects.all(), 
  11.   object_id = author_id, 
  12.  
  13. # Record the last accessed date. We do this *after* the call 
  14. # to object_detail(), not before it, so that this won't be called 
  15. # unless the Author actually exists. (If the author doesn't exist, 
  16. # object_detail() will raise Http404, and we won't reach this point.) 
  17. now = datetime.datetime.now() 
  18. Author.objects.filter(id=author_id).update(last_accessed=now) 
  19.  
  20. return response 



注意
除非你添加 last_accessed 字段到你的 Author 模型并創建 books/author_detail.html 模板,否則這段代碼不能真正工作。
我們可以用同樣的方法修改通用視圖的返回值。 如果我們想要提供一個供下載用的 純文本版本的author列表,我們可以用下面這個視圖:
 

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


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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正蓝旗| 历史| 略阳县| 邢台市| 宣城市| 日土县| 逊克县| 太仆寺旗| 贡山| 信阳市| 府谷县| 昭觉县| 吐鲁番市| 建阳市| 惠安县| 新泰市| 铜川市| 大足县| 克东县| 怀仁县| 禄丰县| 鹿邑县| 宜良县| 马关县| 宽城| 新郑市| 鄄城县| 淮滨县| 株洲县| 双峰县| 洪江市| 新泰市| 北宁市| 顺义区| 夏河县| 呈贡县| 彩票| 大丰市| 洛隆县| 静宁县| 桐庐县|