類別視圖

巴扎黑
發布: 2017-06-23 16:30:43
原創
1624 人瀏覽過

 

類別檢視

#使用原則
  • 程式碼越少越好

  • 永遠不要重複程式碼

  • View應只包含呈現邏輯, 不應包含業務邏輯

  • 保持view邏輯清晰簡單

  • 不要將CBVs用作403, 404, 500的錯誤處理程序

  • 。 mixin簡單明了

 
 
mixin
##在程式設計中mixin是指為繼承它的class提供額外的功能, 但它本身卻不能單獨使用的類別
在具有多繼承能力的程式語言中, mixin可以為類別增加額外功能或方法. 在Django中, 我們可以使用mixin為CBVs提供更多的擴展性, 當然在類別繼承過程中, 我們推薦以下原則:
  • #Django本身提供的View永遠在最右邊

  • mixin依序在上述view的左邊

  • #mixin永遠繼承自Python的object類型

Django mixin函式庫:
 
 
#常用的類別檢視
View                    基本View 可以在任何时候使用 见后面详细介绍
RedirectView            重新定向到其他URL 将访问"/log-in/"的用户重新定向到"/login/"TemplateView            显示Django 模板 一般网站中使用模板显示的页
ListView                显示对象列表 文章列表页
DetailView              显示对象详情 文章详细页
FormView                提交From 网站联系我们或emai订阅form
CreateView              创建对象 创建新文章页
UpdateView              更新对象 修改文章页
DeleteView              删除对象 删除文章页
Generic date views      显示一段时间内的对象 按时间归类的博客
登入後複製

 

#View

django.views.generic.base.View
基於類別的基礎視圖的核心。其它所有的基於類別的視圖都繼承自這個基礎類別。
 
 django.shortcuts  django.views.generic state =http_method_names = [, , , , , , ,  dispatch(self, request, *args, **(= super(Index,self).dispatch(request, *args, **( HttpResponse( HttpResponse()
登入後複製
 
## 
TemplateView

從下列視圖繼承方法與屬性
  1. ##django

    .views<span class="pln">.<span class="pun">generic<span class="pln">.<span class="pun">base<span class="kwd">.<span class="pun">TemplateResponseMixin<span class="pln"><span class="pun"><span class="typ"></span></span></span></span></span></span></span></span></span>

  2. ##django

    .views<span class="pln">.<span class="pun">generic<span class="pln">.<span class="pun">base<span class="kwd">.<span class="pun">ContextMixin<span class="pln"><span class="pun"><span class="typ"></span></span></span></span></span></span></span></span></span>

  3. <span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="kwd"><span class="pun"><span class="pln"><span class="pun"><span class="typ"></span></span></span></span></span></span></span></span></span>

django
.
views
.

generic

.
base
.
View

  1. <span class="pln"><span class="pun"><span class="pln"> <span class="pun"><span class="kwd">範例<span class="pun"><span class="pln"><span class="pun"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> django.views.generic.base =  dispatch(self, request, *args, **(= super(HomePageView,self).dispatch(request, *args, **( get_context_data(self, **= super(HomePageView, self).get_context_data(**] = [,,( context</pre><div class="contentsignin">登入後複製</div></div> <span class="typ"></span> </span></span></span> </span>## </span></span></span></span>

# #RedirectView
從下列視圖繼承方法與屬性:

django

.views<span class="typ">.<span class="pun">generic<span class="pln">.<span class="pun">base<span class="pln">.<span class="pun">View</span></span></span></span></span></span>

############# ###############
 django.views.generic.base == False  query_string = True  pattern_name =    dispatch(self, request, *args, **(= super(ArticleRedirectView,self).dispatch(request, *args, **( get_redirect_url(self, *args, **( super(ArticleRedirectView, self).get_redirect_url(*args, **kwargs)
登入後複製
###### ############ ############# ######### ###### ###### # ###### ######DetaliView#############DetailView###是對這些出版商的諸多的###object###中的一個進行詳細解析,它獲得的主資料是一個###object###。 ##################################從下列視圖繼承方法和屬性:######
  1. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">SingleObjectTemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  2. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">TemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  3. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">BaseDetailView</span></span></span></span></span></span></span></span></span>

  4. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">detail<span class="pun">.<span class="typ">SingleObjectMixin</span></span></span></span></span></span></span></span></span>

  5. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">View</span></span></span></span></span></span></span></span></span>

 
示例
 django.contrib.auth.mixins  chouti.models ==   pk_url_kwarg =   

 get_context_data(self, **(context = super(UserDetailView, self).get_context_data(**] = [33, 44]
登入後複製

 

 

ListView

对于ListView来说,它需要显示的是多个object的信息,返回的是一个对象列表
它的数据来源是model和queryset这两个属性:
  1. <span class="pln">model<span class="pun">:获取这个<span class="pln">model<span class="pun">相关的所有数据</span></span></span></span>

  2. <span class="pln">queryset<span class="pun">:从这个<span class="pln">queryset<span class="pun">中获取指定的数据。</span></span></span></span>

  3. <span class="pln">queryset<span class="pun">的优先级高于<span class="pln">model</span></span></span>

  4. <span class="pun">它所获取的数据是通过<span class="pln">context<span class="pun">中<span class="pln">object_list<span class="pun">这个变量来传递的</span></span></span></span></span>

 
 
从以下视图继承方法和属性:
  1. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">MultipleObjectTemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  2. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">base<span class="pun">.<span class="typ">TemplateResponseMixin</span></span></span></span></span></span></span></span></span>

  3. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">BaseListView</span></span></span></span></span></span></span></span></span>

  4. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">generic<span class="pun">.<span class="pln">list<span class="pun">.<span class="typ">MultipleObjectMixin</span></span></span></span></span></span></span></span></span>

  5. <span class="pln">django<span class="pun">.<span class="pln">views<span class="pun">.<span class="pln">gen</span></span></span></span></span>

 
 
示例
 blog.models  django.views.generic model = Post   queryset = Post.objects.filter(title__contains = )  context_object_name =   template_name=   ( Post.objects.filter(title__contains = self.kwargs[ get_context_data(self, **(= super(PublisherList, self).get_context_data(**kwargs)  context[] = [, ]    context
登入後複製

 

 
 

以上是類別視圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!