exceptions.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. =================
  2. Django Exceptions
  3. =================
  4. Django raises some of its own exceptions as well as standard Python exceptions.
  5. Django Core Exceptions
  6. ======================
  7. .. module:: django.core.exceptions
  8. :synopsis: Django core exceptions
  9. Django core exception classes are defined in ``django.core.exceptions``.
  10. ``ObjectDoesNotExist``
  11. ----------------------
  12. .. exception:: ObjectDoesNotExist
  13. The base class for :exc:`~django.db.models.Model.DoesNotExist` exceptions;
  14. a ``try/except`` for ``ObjectDoesNotExist`` will catch
  15. :exc:`~django.db.models.Model.DoesNotExist` exceptions for all models.
  16. See :meth:`~django.db.models.query.QuerySet.get()` for further information
  17. on :exc:`ObjectDoesNotExist` and :exc:`~django.db.models.Model.DoesNotExist`.
  18. ``FieldDoesNotExist``
  19. ---------------------
  20. .. exception:: FieldDoesNotExist
  21. The ``FieldDoesNotExist`` exception is raised by a model's
  22. ``_meta.get_field()`` method when the requested field does not exist on the
  23. model or on the model's parents.
  24. ``MultipleObjectsReturned``
  25. ---------------------------
  26. .. exception:: MultipleObjectsReturned
  27. The :exc:`MultipleObjectsReturned` exception is raised by a query if only
  28. one object is expected, but multiple objects are returned. A base version
  29. of this exception is provided in :mod:`django.core.exceptions`; each model
  30. class contains a subclassed version that can be used to identify the
  31. specific object type that has returned multiple objects.
  32. See :meth:`~django.db.models.query.QuerySet.get()` for further information.
  33. ``SuspiciousOperation``
  34. -----------------------
  35. .. exception:: SuspiciousOperation
  36. The :exc:`SuspiciousOperation` exception is raised when a user has
  37. performed an operation that should be considered suspicious from a security
  38. perspective, such as tampering with a session cookie. Subclasses of
  39. ``SuspiciousOperation`` include:
  40. * ``DisallowedHost``
  41. * ``DisallowedModelAdminLookup``
  42. * ``DisallowedModelAdminToField``
  43. * ``DisallowedRedirect``
  44. * ``InvalidSessionKey``
  45. * ``SuspiciousFileOperation``
  46. * ``SuspiciousMultipartForm``
  47. * ``SuspiciousSession``
  48. If a ``SuspiciousOperation`` exception reaches the WSGI handler level it is
  49. logged at the ``Error`` level and results in
  50. a :class:`~django.http.HttpResponseBadRequest`. See the :doc:`logging
  51. documentation </topics/logging/>` for more information.
  52. ``PermissionDenied``
  53. --------------------
  54. .. exception:: PermissionDenied
  55. The :exc:`PermissionDenied` exception is raised when a user does not have
  56. permission to perform the action requested.
  57. ``ViewDoesNotExist``
  58. --------------------
  59. .. exception:: ViewDoesNotExist
  60. The :exc:`ViewDoesNotExist` exception is raised by
  61. :mod:`django.urls` when a requested view does not exist.
  62. ``MiddlewareNotUsed``
  63. ---------------------
  64. .. exception:: MiddlewareNotUsed
  65. The :exc:`MiddlewareNotUsed` exception is raised when a middleware is not
  66. used in the server configuration.
  67. ``ImproperlyConfigured``
  68. ------------------------
  69. .. exception:: ImproperlyConfigured
  70. The :exc:`ImproperlyConfigured` exception is raised when Django is
  71. somehow improperly configured -- for example, if a value in ``settings.py``
  72. is incorrect or unparseable.
  73. ``FieldError``
  74. --------------
  75. .. exception:: FieldError
  76. The :exc:`FieldError` exception is raised when there is a problem with a
  77. model field. This can happen for several reasons:
  78. - A field in a model clashes with a field of the same name from an
  79. abstract base class
  80. - An infinite loop is caused by ordering
  81. - A keyword cannot be parsed from the filter parameters
  82. - A field cannot be determined from a keyword in the query
  83. parameters
  84. - A join is not permitted on the specified field
  85. - A field name is invalid
  86. - A query contains invalid order_by arguments
  87. ``ValidationError``
  88. -------------------
  89. .. exception:: ValidationError
  90. The :exc:`ValidationError` exception is raised when data fails form or
  91. model field validation. For more information about validation, see
  92. :doc:`Form and Field Validation </ref/forms/validation>`,
  93. :ref:`Model Field Validation <validating-objects>` and the
  94. :doc:`Validator Reference </ref/validators>`.
  95. ``NON_FIELD_ERRORS``
  96. ~~~~~~~~~~~~~~~~~~~~
  97. .. data:: NON_FIELD_ERRORS
  98. ``ValidationError``\s that don't belong to a particular field in a form
  99. or model are classified as ``NON_FIELD_ERRORS``. This constant is used
  100. as a key in dictionaries that otherwise map fields to their respective
  101. list of errors.
  102. .. currentmodule:: django.urls
  103. URL Resolver exceptions
  104. =======================
  105. URL Resolver exceptions are defined in ``django.urls``.
  106. .. deprecated:: 1.10
  107. In older versions, these exceptions are located in
  108. ``django.core.urlresolvers``. Importing from the old location will continue
  109. to work until Django 2.0.
  110. ``Resolver404``
  111. ---------------
  112. .. exception:: Resolver404
  113. The :exc:`Resolver404` exception is raised by
  114. :func:`~django.urls.resolve()` if the path passed to ``resolve()`` doesn't
  115. map to a view. It's a subclass of :class:`django.http.Http404`.
  116. ``NoReverseMatch``
  117. ------------------
  118. .. exception:: NoReverseMatch
  119. The :exc:`NoReverseMatch` exception is raised by :mod:`django.urls` when a
  120. matching URL in your URLconf cannot be identified based on the parameters
  121. supplied.
  122. .. currentmodule:: django.db
  123. Database Exceptions
  124. ===================
  125. Database exceptions may be imported from ``django.db``.
  126. Django wraps the standard database exceptions so that your Django code has a
  127. guaranteed common implementation of these classes.
  128. .. exception:: Error
  129. .. exception:: InterfaceError
  130. .. exception:: DatabaseError
  131. .. exception:: DataError
  132. .. exception:: OperationalError
  133. .. exception:: IntegrityError
  134. .. exception:: InternalError
  135. .. exception:: ProgrammingError
  136. .. exception:: NotSupportedError
  137. The Django wrappers for database exceptions behave exactly the same as
  138. the underlying database exceptions. See :pep:`249`, the Python Database API
  139. Specification v2.0, for further information.
  140. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
  141. (underlying) database exception, allowing access to any additional
  142. information provided. (Note that this attribute is available under
  143. both Python 2 and Python 3, although :pep:`3134` normally only applies
  144. to Python 3. To avoid unexpected differences with Python 3, Django will also
  145. ensure that the exception made available via ``__cause__`` has a usable
  146. ``__traceback__`` attribute.)
  147. .. versionchanged:: 1.10
  148. The ``__traceback__`` attribute described above was added.
  149. .. exception:: models.ProtectedError
  150. Raised to prevent deletion of referenced objects when using
  151. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
  152. of :exc:`IntegrityError`.
  153. .. currentmodule:: django.http
  154. Http Exceptions
  155. ===============
  156. Http exceptions may be imported from ``django.http``.
  157. ``UnreadablePostError``
  158. -----------------------
  159. .. exception:: UnreadablePostError
  160. :exc:`UnreadablePostError` is raised when a user cancels an upload.
  161. Transaction Exceptions
  162. ======================
  163. .. currentmodule:: django.db.transaction
  164. Transaction exceptions are defined in ``django.db.transaction``.
  165. ``TransactionManagementError``
  166. ------------------------------
  167. .. exception:: TransactionManagementError
  168. :exc:`TransactionManagementError` is raised for any and all problems
  169. related to database transactions.
  170. .. currentmodule:: django.test
  171. Testing Framework Exceptions
  172. ============================
  173. Exceptions provided by the ``django.test`` package.
  174. ``RedirectCycleError``
  175. ----------------------
  176. .. exception:: client.RedirectCycleError
  177. :exc:`~client.RedirectCycleError` is raised when the test client detects a
  178. loop or an overly long chain of redirects.
  179. Python Exceptions
  180. =================
  181. Django raises built-in Python exceptions when appropriate as well. See the
  182. Python documentation for further information on the :ref:`bltin-exceptions`.