index.txt 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. =================
  2. Class-based views
  3. =================
  4. Class-based views API reference. For introductory material, see
  5. :doc:`/topics/class-based-views/index`.
  6. .. toctree::
  7. :maxdepth: 3
  8. base
  9. generic-display
  10. generic-editing
  11. generic-date-based
  12. mixins
  13. flattened-index
  14. Specification
  15. -------------
  16. Each request served by a class-based view has an independent state; therefore,
  17. it is safe to store state variables on the instance (i.e., ``self.foo = 3`` is
  18. a thread-safe operation).
  19. A class-based view is deployed into a URL pattern using the
  20. :meth:`~django.views.generic.base.View.as_view()` classmethod::
  21. urlpatterns = patterns('',
  22. (r'^view/$', MyView.as_view(size=42)),
  23. )
  24. .. admonition:: Thread safety with view arguments
  25. Arguments passed to a view are shared between every instance of a view.
  26. This means that you shoudn't use a list, dictionary, or any other
  27. mutable object as an argument to a view. If you do and the shared object
  28. is modified, the actions of one user visiting your view could have an
  29. effect on subsequent users visiting the same view.
  30. Any argument passed into :meth:`~django.views.generic.base.View.as_view()` will
  31. be assigned onto the instance that is used to service a request. Using the
  32. previous example, this means that every request on ``MyView`` is able to use
  33. ``self.size``.
  34. Base vs Generic views
  35. ---------------------
  36. Base class-based views can be thought of as *parent* views, which can be
  37. used by themselves or inherited from. They may not provide all the
  38. capabilities required for projects, in which case there are Mixins which
  39. extend what base views can do.
  40. Django’s generic views are built off of those base views, and were developed
  41. as a shortcut for common usage patterns such as displaying the details of an
  42. object. They take certain common idioms and patterns found in view
  43. development and abstract them so that you can quickly write common views of
  44. data without having to repeat yourself.
  45. Most generic views require the ``queryset`` key, which is a ``QuerySet``
  46. instance; see :doc:`/topics/db/queries` for more information about ``QuerySet``
  47. objects.