exceptions.txt 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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:`Model.DoesNotExist
  20. <django.db.models.Model.DoesNotExist>` exceptions. A ``try/except`` for
  21. ``ObjectDoesNotExist`` will catch
  22. :exc:`~django.db.models.Model.DoesNotExist` exceptions for all models.
  23. See :meth:`~django.db.models.query.QuerySet.get()`.
  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. ``FieldDoesNotExist``
  31. ---------------------
  32. .. exception:: FieldDoesNotExist
  33. The ``FieldDoesNotExist`` exception is raised by a model's
  34. ``_meta.get_field()`` method when the requested field does not exist on the
  35. model or on the model's parents.
  36. ``MultipleObjectsReturned``
  37. ---------------------------
  38. .. exception:: MultipleObjectsReturned
  39. The base class for :exc:`Model.MultipleObjectsReturned
  40. <django.db.models.Model.MultipleObjectsReturned>` exceptions. A
  41. ``try/except`` for ``MultipleObjectsReturned`` will catch
  42. :exc:`~django.db.models.Model.MultipleObjectsReturned` exceptions for all
  43. models.
  44. See :meth:`~django.db.models.query.QuerySet.get()`.
  45. ``SuspiciousOperation``
  46. -----------------------
  47. .. exception:: SuspiciousOperation
  48. The :exc:`SuspiciousOperation` exception is raised when a user has
  49. performed an operation that should be considered suspicious from a security
  50. perspective, such as tampering with a session cookie. Subclasses of
  51. ``SuspiciousOperation`` include:
  52. * ``DisallowedHost``
  53. * ``DisallowedModelAdminLookup``
  54. * ``DisallowedModelAdminToField``
  55. * ``DisallowedRedirect``
  56. * ``InvalidSessionKey``
  57. * ``RequestDataTooBig``
  58. * ``SuspiciousFileOperation``
  59. * ``SuspiciousMultipartForm``
  60. * ``SuspiciousSession``
  61. * ``TooManyFieldsSent``
  62. If a ``SuspiciousOperation`` exception reaches the WSGI handler level it is
  63. logged at the ``Error`` level and results in
  64. a :class:`~django.http.HttpResponseBadRequest`. See the :doc:`logging
  65. documentation </topics/logging/>` for more information.
  66. ``PermissionDenied``
  67. --------------------
  68. .. exception:: PermissionDenied
  69. The :exc:`PermissionDenied` exception is raised when a user does not have
  70. permission to perform the action requested.
  71. ``ViewDoesNotExist``
  72. --------------------
  73. .. exception:: ViewDoesNotExist
  74. The :exc:`ViewDoesNotExist` exception is raised by
  75. :mod:`django.urls` when a requested view does not exist.
  76. ``MiddlewareNotUsed``
  77. ---------------------
  78. .. exception:: MiddlewareNotUsed
  79. The :exc:`MiddlewareNotUsed` exception is raised when a middleware is not
  80. used in the server configuration.
  81. ``ImproperlyConfigured``
  82. ------------------------
  83. .. exception:: ImproperlyConfigured
  84. The :exc:`ImproperlyConfigured` exception is raised when Django is
  85. somehow improperly configured -- for example, if a value in ``settings.py``
  86. is incorrect or unparseable.
  87. ``FieldError``
  88. --------------
  89. .. exception:: FieldError
  90. The :exc:`FieldError` exception is raised when there is a problem with a
  91. model field. This can happen for several reasons:
  92. - A field in a model clashes with a field of the same name from an
  93. abstract base class
  94. - An infinite loop is caused by ordering
  95. - A keyword cannot be parsed from the filter parameters
  96. - A field cannot be determined from a keyword in the query
  97. parameters
  98. - A join is not permitted on the specified field
  99. - A field name is invalid
  100. - A query contains invalid order_by arguments
  101. ``ValidationError``
  102. -------------------
  103. .. exception:: ValidationError
  104. The :exc:`ValidationError` exception is raised when data fails form or
  105. model field validation. For more information about validation, see
  106. :doc:`Form and Field Validation </ref/forms/validation>`,
  107. :ref:`Model Field Validation <validating-objects>` and the
  108. :doc:`Validator Reference </ref/validators>`.
  109. ``NON_FIELD_ERRORS``
  110. ~~~~~~~~~~~~~~~~~~~~
  111. .. data:: NON_FIELD_ERRORS
  112. ``ValidationError``\s that don't belong to a particular field in a form
  113. or model are classified as ``NON_FIELD_ERRORS``. This constant is used
  114. as a key in dictionaries that otherwise map fields to their respective
  115. list of errors.
  116. ``BadRequest``
  117. --------------
  118. .. exception:: BadRequest
  119. .. versionadded:: 3.2
  120. The :exc:`BadRequest` exception is raised when the request cannot be
  121. processed due to a client error. If a ``BadRequest`` exception reaches the
  122. ASGI/WSGI handler level it results in a
  123. :class:`~django.http.HttpResponseBadRequest`.
  124. ``RequestAborted``
  125. ------------------
  126. .. exception:: RequestAborted
  127. The :exc:`RequestAborted` exception is raised when a HTTP body being read
  128. in by the handler is cut off midstream and the client connection closes,
  129. or when the client does not send data and hits a timeout where the server
  130. closes the connection.
  131. It is internal to the HTTP handler modules and you are unlikely to see
  132. it elsewhere. If you are modifying HTTP handling code, you should raise
  133. this when you encounter an aborted request to make sure the socket is
  134. closed cleanly.
  135. ``SynchronousOnlyOperation``
  136. ----------------------------
  137. .. exception:: SynchronousOnlyOperation
  138. The :exc:`SynchronousOnlyOperation` exception is raised when code that
  139. is only allowed in synchronous Python code is called from an asynchronous
  140. context (a thread with a running asynchronous event loop). These parts of
  141. Django are generally heavily reliant on thread-safety to function and don't
  142. work correctly under coroutines sharing the same thread.
  143. If you are trying to call code that is synchronous-only from an
  144. asynchronous thread, then create a synchronous thread and call it in that.
  145. You can accomplish this is with :func:`asgiref.sync.sync_to_async`.
  146. .. currentmodule:: django.urls
  147. URL Resolver exceptions
  148. =======================
  149. URL Resolver exceptions are defined in ``django.urls``.
  150. ``Resolver404``
  151. ---------------
  152. .. exception:: Resolver404
  153. The :exc:`Resolver404` exception is raised by
  154. :func:`~django.urls.resolve()` if the path passed to ``resolve()`` doesn't
  155. map to a view. It's a subclass of :class:`django.http.Http404`.
  156. ``NoReverseMatch``
  157. ------------------
  158. .. exception:: NoReverseMatch
  159. The :exc:`NoReverseMatch` exception is raised by :mod:`django.urls` when a
  160. matching URL in your URLconf cannot be identified based on the parameters
  161. supplied.
  162. .. currentmodule:: django.db
  163. Database Exceptions
  164. ===================
  165. Database exceptions may be imported from ``django.db``.
  166. Django wraps the standard database exceptions so that your Django code has a
  167. guaranteed common implementation of these classes.
  168. .. exception:: Error
  169. .. exception:: InterfaceError
  170. .. exception:: DatabaseError
  171. .. exception:: DataError
  172. .. exception:: OperationalError
  173. .. exception:: IntegrityError
  174. .. exception:: InternalError
  175. .. exception:: ProgrammingError
  176. .. exception:: NotSupportedError
  177. The Django wrappers for database exceptions behave exactly the same as
  178. the underlying database exceptions. See :pep:`249`, the Python Database API
  179. Specification v2.0, for further information.
  180. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
  181. (underlying) database exception, allowing access to any additional
  182. information provided.
  183. .. exception:: models.ProtectedError
  184. Raised to prevent deletion of referenced objects when using
  185. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
  186. of :exc:`IntegrityError`.
  187. .. exception:: models.RestrictedError
  188. Raised to prevent deletion of referenced objects when using
  189. :attr:`django.db.models.RESTRICT`. :exc:`models.RestrictedError` is a subclass
  190. of :exc:`IntegrityError`.
  191. .. currentmodule:: django.http
  192. Http Exceptions
  193. ===============
  194. Http exceptions may be imported from ``django.http``.
  195. ``UnreadablePostError``
  196. -----------------------
  197. .. exception:: UnreadablePostError
  198. :exc:`UnreadablePostError` is raised when a user cancels an upload.
  199. .. currentmodule:: django.contrib.sessions.exceptions
  200. Sessions Exceptions
  201. ===================
  202. Sessions exceptions are defined in ``django.contrib.sessions.exceptions``.
  203. ``SessionInterrupted``
  204. ----------------------
  205. .. exception:: SessionInterrupted
  206. .. versionadded:: 3.2
  207. :exc:`SessionInterrupted` is raised when a session is destroyed in a
  208. concurrent request. It's a subclass of
  209. :exc:`~django.core.exceptions.BadRequest`.
  210. Transaction Exceptions
  211. ======================
  212. .. currentmodule:: django.db.transaction
  213. Transaction exceptions are defined in ``django.db.transaction``.
  214. ``TransactionManagementError``
  215. ------------------------------
  216. .. exception:: TransactionManagementError
  217. :exc:`TransactionManagementError` is raised for any and all problems
  218. related to database transactions.
  219. .. currentmodule:: django.test
  220. Testing Framework Exceptions
  221. ============================
  222. Exceptions provided by the ``django.test`` package.
  223. ``RedirectCycleError``
  224. ----------------------
  225. .. exception:: client.RedirectCycleError
  226. :exc:`~client.RedirectCycleError` is raised when the test client detects a
  227. loop or an overly long chain of redirects.
  228. Python Exceptions
  229. =================
  230. Django raises built-in Python exceptions when appropriate as well. See the
  231. Python documentation for further information on the :ref:`bltin-exceptions`.