exceptions.txt 8.4 KB

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