exceptions.txt 8.0 KB

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