# 內建基于類的視圖的API #
基于類的視圖的API 參考。另請參見[基于類的視圖](http://python.usyiyi.cn/django/topics/class-based-views/index.html) 的簡介。
+ [基礎視圖](http://python.usyiyi.cn/django/ref/class-based-views/base.html)
+ [View](http://python.usyiyi.cn/django/ref/class-based-views/base.html#view)
+ [TemplateView](http://python.usyiyi.cn/django/ref/class-based-views/base.html#templateview)
+ [RedirectView](http://python.usyiyi.cn/django/ref/class-based-views/base.html#redirectview)
+ [通用的顯示視圖](http://python.usyiyi.cn/django/ref/class-based-views/generic-display.html)
+ [DetailView](http://python.usyiyi.cn/django/ref/class-based-views/generic-display.html#detailview)
+ [ListView](http://python.usyiyi.cn/django/ref/class-based-views/generic-display.html#listview)
+ [通用的編輯視圖](http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html)
+ [FormView](http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html#formview)
+ [CreateView](http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html#createview)
+ [UpdateView](http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html#updateview)
+ [DeleteView](http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html#deleteview)
+ [通用的日期視圖](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html)
+ [ArchiveIndexView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#archiveindexview)
+ [YearArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#yeararchiveview)
+ [MonthArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#montharchiveview)
+ [WeekArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#weekarchiveview)
+ [DayArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#dayarchiveview)
+ [TodayArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#todayarchiveview)
+ [DateDetailView](http://python.usyiyi.cn/django/ref/class-based-views/generic-date-based.html#datedetailview)
+ [基于類的視圖的Mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins.html)
+ [Simple mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins-simple.html)
+ [ContextMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-simple.html#contextmixin)
+ [TemplateResponseMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-simple.html#templateresponsemixin)
+ [Single object mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins-single-object.html)
+ [SingleObjectMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-single-object.html#singleobjectmixin)
+ [SingleObjectTemplateResponseMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-single-object.html#singleobjecttemplateresponsemixin)
+ [Multiple object mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins-multiple-object.html)
+ [MultipleObjectMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-multiple-object.html#multipleobjectmixin)
+ [MultipleObjectTemplateResponseMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-multiple-object.html#multipleobjecttemplateresponsemixin)
+ [Editing mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins-editing.html)
+ [FormMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-editing.html#formmixin)
+ [ModelFormMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-editing.html#modelformmixin)
+ [ProcessFormView](http://python.usyiyi.cn/django/ref/class-based-views/mixins-editing.html#processformview)
+ [DeletionMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-editing.html#deletionmixin)
+ [Date-based mixins](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html)
+ [YearMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#yearmixin)
+ [MonthMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#monthmixin)
+ [DayMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#daymixin)
+ [WeekMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#weekmixin)
+ [DateMixin](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#datemixin)
+ [BaseDateListView](http://python.usyiyi.cn/django/ref/class-based-views/mixins-date-based.html#basedatelistview)
+ [基于類的通用視圖 —— 索引](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html)
+ [Simple generic views](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#simple-generic-views)
+ [View](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#view)
+ [TemplateView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#templateview)
+ [RedirectView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#redirectview)
+ [Detail Views](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#detail-views)
+ [DetailView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#detailview)
+ [List Views](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#list-views)
+ [ListView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#listview)
+ [Editing views](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#editing-views)
+ [FormView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#formview)
+ [CreateView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#createview)
+ [UpdateView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#updateview)
+ [DeleteView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#deleteview)
+ [Date-based views](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#date-based-views)
+ [ArchiveIndexView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#archiveindexview)
+ [YearArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#yeararchiveview)
+ [MonthArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#montharchiveview)
+ [WeekArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#weekarchiveview)
+ [DayArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#dayarchiveview)
+ [TodayArchiveView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#todayarchiveview)
+ [DateDetailView](http://python.usyiyi.cn/django/ref/class-based-views/flattened-index.html#datedetailview)
## 說明 ##
由基于類的視圖處理的每個請求都具有一個獨立的狀態;所以,在實例中保存狀態變量是安全的(例如,`self.foo = 3` 是線程安全的操作)。
基于類的視圖在URL 模式中的部署使用`as_view()` 類方法:
```
urlpatterns = [
url(r'^view/$', MyView.as_view(size=42)),
]
```
> 視圖參數的線程安全性
>
> 傳遞給視圖的參數在視圖的每個實例之間共享。這表示不應該使用列表、字典或其它可變對象作為視圖的參數。如果你真這么做而且對共享的對象做過修改,某個用戶的行為可能對后面訪問同一個視圖的用戶產生影響。
傳遞給`as_view()` 的參數將賦值給服務請求的實例。利用前面的例子,這表示對`MyView `的每個請求都可以使用 `self.size`。參數必須對應于在類中已經存在的屬性(`hasattr` 檢查可以返回`True`)。
## 基礎視圖 VS. 通用視圖 ##
基于類的基礎視圖可以認為是父視圖,它們可以直接使用或者繼承它們。它們不能滿足項目中所有的需求,在這種情況下有Mixin 可以擴展基礎視圖的功能。
Django 的通用視圖建立在基礎視圖之上,用于作為經常用到的功能的快捷方式,例如顯示對象的詳細信息。它們提煉視圖開發中常見的風格和模式并將它們抽象,這樣你可以快速編寫常見的視圖而不用重復你自己。
大部分通常視圖需要`queryset` 鍵 ,它是一個`查詢集` 實例;關于`查詢集`對象的更多信息,請參見執行查詢。
> 譯者:[Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html),原文:[API reference](https://docs.djangoproject.com/en/1.8/ref/class-based-views/)。
>
> 本文以 [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/cn/) 協議發布,轉載請保留作者署名和文章出處。
>
> [Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html)人手緊缺,有興趣的朋友可以加入我們,完全公益性質。交流群:467338606。
- 新手入門
- 從零開始
- 概覽
- 安裝
- 教程
- 第1部分:模型
- 第2部分:管理站點
- 第3部分:視圖和模板
- 第4部分:表單和通用視圖
- 第5部分:測試
- 第6部分:靜態文件
- 高級教程
- 如何編寫可重用的應用
- 為Django編寫首個補丁
- 模型層
- 模型
- 模型語法
- 元選項
- 模型類
- 查詢集
- 執行查詢
- 查找表達式
- 模型的實例
- 實例方法
- 訪問關聯對象
- 遷移
- 模式編輯器
- 編寫遷移
- 高級
- 管理器
- 原始的SQL查詢
- 聚合
- 多數據庫
- 自定義查找
- 條件表達式
- 數據庫函數
- 其它
- 遺留的數據庫
- 提供初始數據
- 優化數據庫訪問
- 視圖層
- 基礎
- URL配置
- 視圖函數
- 快捷函數
- 裝飾器
- 參考
- 內建的視圖
- TemplateResponse 對象
- 文件上傳
- 概覽
- File 對象
- 儲存API
- 管理文件
- 自定義存儲
- 基于類的視圖
- 概覽
- 內建顯示視圖
- 內建編輯視圖
- API參考
- 分類索引
- 高級
- 生成 CSV
- 生成 PDF
- 中間件
- 概覽
- 內建的中間件類
- 模板層
- 基礎
- 面向設計師
- 語言概覽
- 人性化
- 面向程序員
- 表單
- 基礎
- 概覽
- 表單API
- 內建的Widget
- 高級
- 整合媒體
- 開發過程
- 設置
- 概覽
- 應用程序
- 異常
- 概覽
- django-admin 和 manage.py
- 添加自定義的命令
- 測試
- 介紹
- 部署
- 概述
- WSGI服務器
- 部署靜態文件
- 通過email追蹤代碼錯誤
- Admin
- 管理操作
- 管理文檔生成器
- 安全
- 安全概述
- 說明Django中的安全問題
- 點擊劫持保護
- 加密簽名
- 國際化和本地化
- 概述
- 本地化WEB UI格式化輸入
- “本地特色”
- 常見的網站應用工具
- 認證
- 概覽
- 使用認證系統
- 密碼管理
- 日志
- 分頁
- 會話
- 數據驗證
- 其它核心功能
- 按需內容處理
- 重定向
- 信號
- 系統檢查框架