2014-08-24 07:30:14 +08:00
|
|
|
==============================
|
2015-08-19 04:01:36 +08:00
|
|
|
Built-in class-based views API
|
2014-08-24 07:30:14 +08:00
|
|
|
==============================
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2014-09-28 06:12:34 +08:00
|
|
|
Class-based views API reference. For introductory material, see the
|
|
|
|
:doc:`/topics/class-based-views/index` topic guide.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. toctree::
|
2012-08-11 14:07:15 +08:00
|
|
|
:maxdepth: 3
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
base
|
|
|
|
generic-display
|
|
|
|
generic-editing
|
|
|
|
generic-date-based
|
|
|
|
mixins
|
2012-09-01 21:20:38 +08:00
|
|
|
flattened-index
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Specification
|
2016-01-03 18:56:22 +08:00
|
|
|
=============
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Each request served by a class-based view has an independent state; therefore,
|
|
|
|
it is safe to store state variables on the instance (i.e., ``self.foo = 3`` is
|
|
|
|
a thread-safe operation).
|
|
|
|
|
|
|
|
A class-based view is deployed into a URL pattern using the
|
2012-09-09 01:19:58 +08:00
|
|
|
:meth:`~django.views.generic.base.View.as_view()` classmethod::
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2016-10-21 01:29:04 +08:00
|
|
|
path('view/', MyView.as_view(size=42)),
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. admonition:: Thread safety with view arguments
|
|
|
|
|
|
|
|
Arguments passed to a view are shared between every instance of a view.
|
2013-05-22 03:29:14 +08:00
|
|
|
This means that you shouldn't use a list, dictionary, or any other
|
2012-08-10 04:22:22 +08:00
|
|
|
mutable object as an argument to a view. If you do and the shared object
|
|
|
|
is modified, the actions of one user visiting your view could have an
|
|
|
|
effect on subsequent users visiting the same view.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-11-22 22:07:21 +08:00
|
|
|
Arguments passed into :meth:`~django.views.generic.base.View.as_view()` will
|
2012-09-09 01:19:58 +08:00
|
|
|
be assigned onto the instance that is used to service a request. Using the
|
|
|
|
previous example, this means that every request on ``MyView`` is able to use
|
2012-11-22 22:07:21 +08:00
|
|
|
``self.size``. Arguments must correspond to attributes that already exist on
|
|
|
|
the class (return ``True`` on a ``hasattr`` check).
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Base vs Generic views
|
2016-01-03 18:56:22 +08:00
|
|
|
=====================
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Base class-based views can be thought of as *parent* views, which can be
|
|
|
|
used by themselves or inherited from. They may not provide all the
|
|
|
|
capabilities required for projects, in which case there are Mixins which
|
|
|
|
extend what base views can do.
|
|
|
|
|
2017-07-18 20:24:49 +08:00
|
|
|
Django's generic views are built off of those base views, and were developed
|
2012-06-11 16:34:00 +08:00
|
|
|
as a shortcut for common usage patterns such as displaying the details of an
|
|
|
|
object. They take certain common idioms and patterns found in view
|
|
|
|
development and abstract them so that you can quickly write common views of
|
|
|
|
data without having to repeat yourself.
|
|
|
|
|
|
|
|
Most generic views require the ``queryset`` key, which is a ``QuerySet``
|
|
|
|
instance; see :doc:`/topics/db/queries` for more information about ``QuerySet``
|
|
|
|
objects.
|