1. =================
    
  2. Django Exceptions
    
  3. =================
    
  4. 
    
  5. Django raises some of its own exceptions as well as standard Python exceptions.
    
  6. 
    
  7. Django Core Exceptions
    
  8. ======================
    
  9. 
    
  10. .. module:: django.core.exceptions
    
  11.     :synopsis: Django core exceptions
    
  12. 
    
  13. Django core exception classes are defined in ``django.core.exceptions``.
    
  14. 
    
  15. ``AppRegistryNotReady``
    
  16. -----------------------
    
  17. 
    
  18. .. exception:: AppRegistryNotReady
    
  19. 
    
  20.     This exception is raised when attempting to use models before the :ref:`app
    
  21.     loading process <app-loading-process>`, which initializes the ORM, is
    
  22.     complete.
    
  23. 
    
  24. ``ObjectDoesNotExist``
    
  25. ----------------------
    
  26. 
    
  27. .. exception:: ObjectDoesNotExist
    
  28. 
    
  29.     The base class for :exc:`Model.DoesNotExist
    
  30.     <django.db.models.Model.DoesNotExist>` exceptions. A ``try/except`` for
    
  31.     ``ObjectDoesNotExist`` will catch
    
  32.     :exc:`~django.db.models.Model.DoesNotExist` exceptions for all models.
    
  33. 
    
  34.     See :meth:`~django.db.models.query.QuerySet.get()`.
    
  35. 
    
  36. ``EmptyResultSet``
    
  37. ------------------
    
  38. 
    
  39. .. exception:: EmptyResultSet
    
  40. 
    
  41.     ``EmptyResultSet`` may be raised during query generation if a query won't
    
  42.     return any results. Most Django projects won't encounter this exception,
    
  43.     but it might be useful for implementing custom lookups and expressions.
    
  44. 
    
  45. ``FieldDoesNotExist``
    
  46. ---------------------
    
  47. 
    
  48. .. exception:: FieldDoesNotExist
    
  49. 
    
  50.     The ``FieldDoesNotExist`` exception is raised by a model's
    
  51.     ``_meta.get_field()`` method when the requested field does not exist on the
    
  52.     model or on the model's parents.
    
  53. 
    
  54. ``MultipleObjectsReturned``
    
  55. ---------------------------
    
  56. 
    
  57. .. exception:: MultipleObjectsReturned
    
  58. 
    
  59.     The base class for :exc:`Model.MultipleObjectsReturned
    
  60.     <django.db.models.Model.MultipleObjectsReturned>` exceptions. A
    
  61.     ``try/except`` for ``MultipleObjectsReturned`` will catch
    
  62.     :exc:`~django.db.models.Model.MultipleObjectsReturned` exceptions for all
    
  63.     models.
    
  64. 
    
  65.     See :meth:`~django.db.models.query.QuerySet.get()`.
    
  66. 
    
  67. ``SuspiciousOperation``
    
  68. -----------------------
    
  69. 
    
  70. .. exception:: SuspiciousOperation
    
  71. 
    
  72.     The :exc:`SuspiciousOperation` exception is raised when a user has
    
  73.     performed an operation that should be considered suspicious from a security
    
  74.     perspective, such as tampering with a session cookie. Subclasses of
    
  75.     ``SuspiciousOperation`` include:
    
  76. 
    
  77.     * ``DisallowedHost``
    
  78.     * ``DisallowedModelAdminLookup``
    
  79.     * ``DisallowedModelAdminToField``
    
  80.     * ``DisallowedRedirect``
    
  81.     * ``InvalidSessionKey``
    
  82.     * ``RequestDataTooBig``
    
  83.     * ``SuspiciousFileOperation``
    
  84.     * ``SuspiciousMultipartForm``
    
  85.     * ``SuspiciousSession``
    
  86.     * ``TooManyFieldsSent``
    
  87.     * ``TooManyFilesSent``
    
  88. 
    
  89.     If a ``SuspiciousOperation`` exception reaches the ASGI/WSGI handler level
    
  90.     it is logged at the ``Error`` level and results in
    
  91.     a :class:`~django.http.HttpResponseBadRequest`. See the :doc:`logging
    
  92.     documentation </topics/logging/>` for more information.
    
  93. 
    
  94. .. versionchanged:: 3.2.18
    
  95. 
    
  96.     ``SuspiciousOperation`` is raised when too many files are submitted.
    
  97. 
    
  98. ``PermissionDenied``
    
  99. --------------------
    
  100. 
    
  101. .. exception:: PermissionDenied
    
  102. 
    
  103.     The :exc:`PermissionDenied` exception is raised when a user does not have
    
  104.     permission to perform the action requested.
    
  105. 
    
  106. ``ViewDoesNotExist``
    
  107. --------------------
    
  108. 
    
  109. .. exception:: ViewDoesNotExist
    
  110. 
    
  111.     The :exc:`ViewDoesNotExist` exception is raised by
    
  112.     :mod:`django.urls` when a requested view does not exist.
    
  113. 
    
  114. ``MiddlewareNotUsed``
    
  115. ---------------------
    
  116. 
    
  117. .. exception:: MiddlewareNotUsed
    
  118. 
    
  119.     The :exc:`MiddlewareNotUsed` exception is raised when a middleware is not
    
  120.     used in the server configuration.
    
  121. 
    
  122. ``ImproperlyConfigured``
    
  123. ------------------------
    
  124. 
    
  125. .. exception:: ImproperlyConfigured
    
  126. 
    
  127.     The :exc:`ImproperlyConfigured` exception is raised when Django is
    
  128.     somehow improperly configured -- for example, if a value in ``settings.py``
    
  129.     is incorrect or unparseable.
    
  130. 
    
  131. ``FieldError``
    
  132. --------------
    
  133. 
    
  134. .. exception:: FieldError
    
  135. 
    
  136.     The :exc:`FieldError` exception is raised when there is a problem with a
    
  137.     model field. This can happen for several reasons:
    
  138. 
    
  139.     - A field in a model clashes with a field of the same name from an
    
  140.       abstract base class
    
  141.     - An infinite loop is caused by ordering
    
  142.     - A keyword cannot be parsed from the filter parameters
    
  143.     - A field cannot be determined from a keyword in the query
    
  144.       parameters
    
  145.     - A join is not permitted on the specified field
    
  146.     - A field name is invalid
    
  147.     - A query contains invalid order_by arguments
    
  148. 
    
  149. ``ValidationError``
    
  150. -------------------
    
  151. 
    
  152. .. exception:: ValidationError
    
  153. 
    
  154.     The :exc:`ValidationError` exception is raised when data fails form or
    
  155.     model field validation. For more information about validation, see
    
  156.     :doc:`Form and Field Validation </ref/forms/validation>`,
    
  157.     :ref:`Model Field Validation <validating-objects>` and the
    
  158.     :doc:`Validator Reference </ref/validators>`.
    
  159. 
    
  160. ``NON_FIELD_ERRORS``
    
  161. ~~~~~~~~~~~~~~~~~~~~
    
  162. 
    
  163. .. data:: NON_FIELD_ERRORS
    
  164. 
    
  165. ``ValidationError``\s that don't belong to a particular field in a form
    
  166. or model are classified as ``NON_FIELD_ERRORS``. This constant is used
    
  167. as a key in dictionaries that otherwise map fields to their respective
    
  168. list of errors.
    
  169. 
    
  170. ``BadRequest``
    
  171. --------------
    
  172. 
    
  173. .. exception:: BadRequest
    
  174. 
    
  175.     The :exc:`BadRequest` exception is raised when the request cannot be
    
  176.     processed due to a client error. If a ``BadRequest`` exception reaches the
    
  177.     ASGI/WSGI handler level it results in a
    
  178.     :class:`~django.http.HttpResponseBadRequest`.
    
  179. 
    
  180. ``RequestAborted``
    
  181. ------------------
    
  182. 
    
  183. .. exception:: RequestAborted
    
  184. 
    
  185.     The :exc:`RequestAborted` exception is raised when an HTTP body being read
    
  186.     in by the handler is cut off midstream and the client connection closes,
    
  187.     or when the client does not send data and hits a timeout where the server
    
  188.     closes the connection.
    
  189. 
    
  190.     It is internal to the HTTP handler modules and you are unlikely to see
    
  191.     it elsewhere. If you are modifying HTTP handling code, you should raise
    
  192.     this when you encounter an aborted request to make sure the socket is
    
  193.     closed cleanly.
    
  194. 
    
  195. ``SynchronousOnlyOperation``
    
  196. ----------------------------
    
  197. 
    
  198. .. exception:: SynchronousOnlyOperation
    
  199. 
    
  200.     The :exc:`SynchronousOnlyOperation` exception is raised when code that
    
  201.     is only allowed in synchronous Python code is called from an asynchronous
    
  202.     context (a thread with a running asynchronous event loop). These parts of
    
  203.     Django are generally heavily reliant on thread-safety to function and don't
    
  204.     work correctly under coroutines sharing the same thread.
    
  205. 
    
  206.     If you are trying to call code that is synchronous-only from an
    
  207.     asynchronous thread, then create a synchronous thread and call it in that.
    
  208.     You can accomplish this is with :func:`asgiref.sync.sync_to_async`.
    
  209. 
    
  210. .. currentmodule:: django.urls
    
  211. 
    
  212. URL Resolver exceptions
    
  213. =======================
    
  214. 
    
  215. URL Resolver exceptions are defined in ``django.urls``.
    
  216. 
    
  217. ``Resolver404``
    
  218. ---------------
    
  219. 
    
  220. .. exception:: Resolver404
    
  221. 
    
  222.     The :exc:`Resolver404` exception is raised by
    
  223.     :func:`~django.urls.resolve()` if the path passed to ``resolve()`` doesn't
    
  224.     map to a view. It's a subclass of :class:`django.http.Http404`.
    
  225. 
    
  226. ``NoReverseMatch``
    
  227. ------------------
    
  228. 
    
  229. .. exception:: NoReverseMatch
    
  230. 
    
  231.     The :exc:`NoReverseMatch` exception is raised by :mod:`django.urls` when a
    
  232.     matching URL in your URLconf cannot be identified based on the parameters
    
  233.     supplied.
    
  234. 
    
  235. .. currentmodule:: django.db
    
  236. 
    
  237. Database Exceptions
    
  238. ===================
    
  239. 
    
  240. Database exceptions may be imported from ``django.db``.
    
  241. 
    
  242. Django wraps the standard database exceptions so that your Django code has a
    
  243. guaranteed common implementation of these classes.
    
  244. 
    
  245. .. exception:: Error
    
  246. .. exception:: InterfaceError
    
  247. .. exception:: DatabaseError
    
  248. .. exception:: DataError
    
  249. .. exception:: OperationalError
    
  250. .. exception:: IntegrityError
    
  251. .. exception:: InternalError
    
  252. .. exception:: ProgrammingError
    
  253. .. exception:: NotSupportedError
    
  254. 
    
  255. The Django wrappers for database exceptions behave exactly the same as
    
  256. the underlying database exceptions. See :pep:`249`, the Python Database API
    
  257. Specification v2.0, for further information.
    
  258. 
    
  259. As per :pep:`3134`, a ``__cause__`` attribute is set with the original
    
  260. (underlying) database exception, allowing access to any additional
    
  261. information provided.
    
  262. 
    
  263. .. exception:: models.ProtectedError
    
  264. 
    
  265. Raised to prevent deletion of referenced objects when using
    
  266. :attr:`django.db.models.PROTECT`. :exc:`models.ProtectedError` is a subclass
    
  267. of :exc:`IntegrityError`.
    
  268. 
    
  269. .. exception:: models.RestrictedError
    
  270. 
    
  271. Raised to prevent deletion of referenced objects when using
    
  272. :attr:`django.db.models.RESTRICT`. :exc:`models.RestrictedError` is a subclass
    
  273. of :exc:`IntegrityError`.
    
  274. 
    
  275. .. currentmodule:: django.http
    
  276. 
    
  277. HTTP Exceptions
    
  278. ===============
    
  279. 
    
  280. HTTP exceptions may be imported from ``django.http``.
    
  281. 
    
  282. ``UnreadablePostError``
    
  283. -----------------------
    
  284. 
    
  285. .. exception:: UnreadablePostError
    
  286. 
    
  287.     :exc:`UnreadablePostError` is raised when a user cancels an upload.
    
  288. 
    
  289. .. currentmodule:: django.contrib.sessions.exceptions
    
  290. 
    
  291. Sessions Exceptions
    
  292. ===================
    
  293. 
    
  294. Sessions exceptions are defined in ``django.contrib.sessions.exceptions``.
    
  295. 
    
  296. ``SessionInterrupted``
    
  297. ----------------------
    
  298. 
    
  299. .. exception:: SessionInterrupted
    
  300. 
    
  301.     :exc:`SessionInterrupted` is raised when a session is destroyed in a
    
  302.     concurrent request. It's a subclass of
    
  303.     :exc:`~django.core.exceptions.BadRequest`.
    
  304. 
    
  305. Transaction Exceptions
    
  306. ======================
    
  307. 
    
  308. .. currentmodule:: django.db.transaction
    
  309. 
    
  310. Transaction exceptions are defined in ``django.db.transaction``.
    
  311. 
    
  312. ``TransactionManagementError``
    
  313. ------------------------------
    
  314. 
    
  315. .. exception:: TransactionManagementError
    
  316. 
    
  317.     :exc:`TransactionManagementError` is raised for any and all problems
    
  318.     related to database transactions.
    
  319. 
    
  320. .. currentmodule:: django.test
    
  321. 
    
  322. Testing Framework Exceptions
    
  323. ============================
    
  324. 
    
  325. Exceptions provided by the ``django.test`` package.
    
  326. 
    
  327. ``RedirectCycleError``
    
  328. ----------------------
    
  329. 
    
  330. .. exception:: client.RedirectCycleError
    
  331. 
    
  332.     :exc:`~client.RedirectCycleError` is raised when the test client detects a
    
  333.     loop or an overly long chain of redirects.
    
  334. 
    
  335. Python Exceptions
    
  336. =================
    
  337. 
    
  338. Django raises built-in Python exceptions when appropriate as well. See the
    
  339. Python documentation for further information on the :ref:`bltin-exceptions`.