ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 内建基于类的视图的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。