2
0

index.txt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ==============================
  2. Built-in class-based views API
  3. ==============================
  4. Class-based views API reference. For introductory material, see the
  5. :doc:`/topics/class-based-views/index` topic guide.
  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 = [
  22. path("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 shouldn'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. Arguments 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``. Arguments must correspond to attributes that already exist on
  34. the class (return ``True`` on a ``hasattr`` check).
  35. Base vs Generic views
  36. =====================
  37. Base class-based views can be thought of as *parent* views, which can be
  38. used by themselves or inherited from. They may not provide all the
  39. capabilities required for projects, in which case there are Mixins which
  40. extend what base views can do.
  41. Django's generic views are built off of those base views, and were developed
  42. as a shortcut for common usage patterns such as displaying the details of an
  43. object. They take certain common idioms and patterns found in view
  44. development and abstract them so that you can quickly write common views of
  45. data without having to repeat yourself.
  46. Most generic views require the ``queryset`` key, which is a ``QuerySet``
  47. instance; see :doc:`/topics/db/queries` for more information about ``QuerySet``
  48. objects.