exceptions.txt 11 KB

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