exceptions.txt 10 KB

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