exceptions.txt 10 KB

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