index.txt 2.0 KB

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