exceptions.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. =================
  2. Django Exceptions
  3. =================
  4. Django raises some Django specific exceptions as well as many standard
  5. Python exceptions.
  6. Django Core Exceptions
  7. ======================
  8. .. module:: django.core.exceptions
  9. :synopsis: Django core exceptions
  10. Django core exception classes are defined in :mod:`django.core.exceptions`.
  11. ObjectDoesNotExist and DoesNotExist
  12. -----------------------------------
  13. .. exception:: DoesNotExist
  14. The ``DoesNotExist`` exception is raised when an object is not found for
  15. the given parameters of a query. Django provides a ``DoesNotExist``
  16. exception as an attribute of each model class to identify the class of
  17. object that could not be found and to allow you to catch a particular model
  18. class with ``try/except``.
  19. .. exception:: ObjectDoesNotExist
  20. The base class for ``DoesNotExist`` exceptions; a ``try/except`` for
  21. ``ObjectDoesNotExist`` will catch ``DoesNotExist`` exceptions for all
  22. models.
  23. See :meth:`~django.db.models.query.QuerySet.get()` for further information
  24. on :exc:`ObjectDoesNotExist` and :exc:`DoesNotExist`.
  25. MultipleObjectsReturned
  26. -----------------------
  27. .. exception:: MultipleObjectsReturned
  28. The :exc:`MultipleObjectsReturned` exception is raised by a query if only
  29. one object is expected, but multiple objects are returned. A base version
  30. of this exception is provided in :mod:`django.core.exceptions`; each model
  31. class contains a subclassed version that can be used to identify the
  32. specific object type that has returned multiple objects.
  33. See :meth:`~django.db.models.query.QuerySet.get()` for further information.
  34. SuspiciousOperation
  35. -------------------
  36. .. exception:: SuspiciousOperation
  37. The :exc:`SuspiciousOperation` exception is raised when a user has
  38. performed an operation that should be considered suspicious from a security
  39. perspective, such as tampering with a session cookie. Subclasses of
  40. SuspiciousOperation include:
  41. * DisallowedHost
  42. * DisallowedModelAdminLookup
  43. * DisallowedModelAdminToField
  44. * DisallowedRedirect
  45. * InvalidSessionKey
  46. * SuspiciousFileOperation
  47. * SuspiciousMultipartForm
  48. * SuspiciousSession
  49. * WizardViewCookieModified
  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.core.urlresolvers` 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.core.urlresolvers
  105. URL Resolver exceptions
  106. =======================
  107. URL Resolver exceptions are defined in :mod:`django.core.urlresolvers`.
  108. Resolver404
  109. --------------
  110. .. exception:: Resolver404
  111. The :exc:`Resolver404` exception is raised by
  112. :func:`django.core.urlresolvers.resolve()` if the path passed to
  113. ``resolve()`` doesn't map to a view. It's a subclass of
  114. :class:`django.http.Http404`
  115. NoReverseMatch
  116. --------------
  117. .. exception:: NoReverseMatch
  118. The :exc:`NoReverseMatch` exception is raised by
  119. :mod:`django.core.urlresolvers` when a matching URL in your URLconf
  120. cannot be identified based on the parameters supplied.
  121. .. currentmodule:: django.db
  122. Database Exceptions
  123. ===================
  124. Database exceptions are provided in :mod:`django.db`.
  125. Django wraps the standard database exceptions so that your Django code has a
  126. guaranteed common implementation of these classes.
  127. .. exception:: Error
  128. .. exception:: InterfaceError
  129. .. exception:: DatabaseError
  130. .. exception:: DataError
  131. .. exception:: OperationalError
  132. .. exception:: IntegrityError
  133. .. exception:: InternalError
  134. .. exception:: ProgrammingError
  135. .. exception:: NotSupportedError
  136. The Django wrappers for database exceptions behave exactly the same as
  137. the underlying database exceptions. See :pep:`249`, the Python Database API
  138. Specification v2.0, for further information.
  139. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
  140. (underlying) database exception, allowing access to any additional
  141. information provided. (Note that this attribute is available under
  142. both Python 2 and Python 3, although :pep:`3134` normally only applies
  143. to Python 3.)
  144. .. exception:: models.ProtectedError
  145. Raised to prevent deletion of referenced objects when using
  146. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
  147. of :exc:`IntegrityError`.
  148. .. currentmodule:: django.http
  149. Http Exceptions
  150. ===============
  151. Http exceptions are provided in :mod:`django.http`.
  152. .. exception:: UnreadablePostError
  153. The :exc:`UnreadablePostError` is raised when a user cancels an upload.
  154. .. currentmodule:: django.db.transaction
  155. Transaction Exceptions
  156. ======================
  157. Transaction exceptions are defined in :mod:`django.db.transaction`.
  158. .. exception:: TransactionManagementError
  159. The :exc:`TransactionManagementError` is raised for any and all problems
  160. related to database transactions.
  161. .. currentmodule:: django.test
  162. Testing Framework Exceptions
  163. ============================
  164. Exceptions provided by the :mod:`django.test` package.
  165. .. exception:: client.RedirectCycleError
  166. .. versionadded:: 1.8
  167. :exc:`~client.RedirectCycleError` is raised when the test client detects a
  168. loop or an overly long chain of redirects.
  169. Python Exceptions
  170. =================
  171. Django raises built-in Python exceptions when appropriate as well. See the
  172. Python documentation for further information on the :ref:`bltin-exceptions`.