exceptions.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * DisallowedRedirect
  44. * InvalidSessionKey
  45. * SuspiciousFileOperation
  46. * SuspiciousMultipartForm
  47. * SuspiciousSession
  48. * WizardViewCookieModified
  49. If a ``SuspiciousOperation`` exception reaches the WSGI handler level it is
  50. logged at the ``Error`` level and results in
  51. a :class:`~django.http.HttpResponseBadRequest`. See the :doc:`logging
  52. documentation </topics/logging/>` for more information.
  53. PermissionDenied
  54. ----------------
  55. .. exception:: PermissionDenied
  56. The :exc:`PermissionDenied` exception is raised when a user does not have
  57. permission to perform the action requested.
  58. ViewDoesNotExist
  59. ----------------
  60. .. exception:: ViewDoesNotExist
  61. The :exc:`ViewDoesNotExist` exception is raised by
  62. :mod:`django.core.urlresolvers` when a requested view does not exist.
  63. MiddlewareNotUsed
  64. -----------------
  65. .. exception:: MiddlewareNotUsed
  66. The :exc:`MiddlewareNotUsed` exception is raised when a middleware is not
  67. used in the server configuration.
  68. ImproperlyConfigured
  69. --------------------
  70. .. exception:: ImproperlyConfigured
  71. The :exc:`ImproperlyConfigured` exception is raised when Django is
  72. somehow improperly configured -- for example, if a value in ``settings.py``
  73. is incorrect or unparseable.
  74. FieldError
  75. ----------
  76. .. exception:: FieldError
  77. The :exc:`FieldError` exception is raised when there is a problem with a
  78. model field. This can happen for several reasons:
  79. - A field in a model clashes with a field of the same name from an
  80. abstract base class
  81. - An infinite loop is caused by ordering
  82. - A keyword cannot be parsed from the filter parameters
  83. - A field cannot be determined from a keyword in the query
  84. parameters
  85. - A join is not permitted on the specified field
  86. - A field name is invalid
  87. - A query contains invalid order_by arguments
  88. ValidationError
  89. ---------------
  90. .. exception:: ValidationError
  91. The :exc:`ValidationError` exception is raised when data fails form or
  92. model field validation. For more information about validation, see
  93. :doc:`Form and Field Validation </ref/forms/validation>`,
  94. :ref:`Model Field Validation <validating-objects>` and the
  95. :doc:`Validator Reference </ref/validators>`.
  96. .. currentmodule:: django.core.urlresolvers
  97. URL Resolver exceptions
  98. =======================
  99. URL Resolver exceptions are defined in :mod:`django.core.urlresolvers`.
  100. Resolver404
  101. --------------
  102. .. exception:: Resolver404
  103. The :exc:`Resolver404` exception is raised by
  104. :func:`django.core.urlresolvers.resolve()` if the path passed to
  105. ``resolve()`` doesn't map to a view. It's a subclass of
  106. :class:`django.http.Http404`
  107. NoReverseMatch
  108. --------------
  109. .. exception:: NoReverseMatch
  110. The :exc:`NoReverseMatch` exception is raised by
  111. :mod:`django.core.urlresolvers` when a matching URL in your URLconf
  112. cannot be identified based on the parameters supplied.
  113. .. currentmodule:: django.db
  114. Database Exceptions
  115. ===================
  116. Database exceptions are provided in :mod:`django.db`.
  117. Django wraps the standard database exceptions so that your Django code has a
  118. guaranteed common implementation of these classes.
  119. .. exception:: Error
  120. .. exception:: InterfaceError
  121. .. exception:: DatabaseError
  122. .. exception:: DataError
  123. .. exception:: OperationalError
  124. .. exception:: IntegrityError
  125. .. exception:: InternalError
  126. .. exception:: ProgrammingError
  127. .. exception:: NotSupportedError
  128. The Django wrappers for database exceptions behave exactly the same as
  129. the underlying database exceptions. See :pep:`249`, the Python Database API
  130. Specification v2.0, for further information.
  131. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
  132. (underlying) database exception, allowing access to any additional
  133. information provided. (Note that this attribute is available under
  134. both Python 2 and Python 3, although :pep:`3134` normally only applies
  135. to Python 3.)
  136. .. versionchanged:: 1.6
  137. Previous versions of Django only wrapped ``DatabaseError`` and
  138. ``IntegrityError``, and did not provide ``__cause__``.
  139. .. exception:: models.ProtectedError
  140. Raised to prevent deletion of referenced objects when using
  141. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
  142. of :exc:`IntegrityError`.
  143. .. currentmodule:: django.http
  144. Http Exceptions
  145. ===============
  146. Http exceptions are provided in :mod:`django.http`.
  147. .. exception:: UnreadablePostError
  148. The :exc:`UnreadablePostError` is raised when a user cancels an upload.
  149. .. currentmodule:: django.db.transaction
  150. Transaction Exceptions
  151. ======================
  152. Transaction exceptions are defined in :mod:`django.db.transaction`.
  153. .. exception:: TransactionManagementError
  154. The :exc:`TransactionManagementError` is raised for any and all problems
  155. related to database transactions.
  156. Python Exceptions
  157. =================
  158. Django raises built-in Python exceptions when appropriate as well. See the
  159. Python documentation for further information on the
  160. built-in :mod:`exceptions`.