Home >>Django Tutorial >Django Exceptions

Django Exceptions

Django Exceptions

An exception is an abnormal event that results in to program failure. To handle this situation, Django uses its own exception classes and it supports all the core Python exceptions as well. These core exceptions classes are defined in the django.core.exceptions module.

Django Exception Classes

Exception Description
AppRegistryNotReady This exception is raised when we attempt to use models before the app loading process.
ObjectDoesNotExist This is the base class for DoesNotExist exceptions.
EmptyResultSet This exception is rased if a query does not return any result.
FieldDoesNotExist This exception is raised when the requested field does not exist.
MultipleObjectsReturned This exception is raised if only one object is expected, but multiple objects are returned.
SuspiciousOperation This exception is raised if a user has performed an operation that is considered suspicious from a security perspective.
PermissionDenied This exception is raised when a user does not have permission to perform the action requested.
ViewDoesNotExist This exception is raised by django.urls when a requested view does not exist.
MiddlewareNotUsed This exception is raised when a middleware is not used in the server configuration.
ImproperlyConfigured This exception is raised when Django is somehow improperly configured.
FieldError This exception is raised when there is a problem with a model field.
ValidationError This exception is raised when data validation fails form or model field validation.

Django URL Resolver Exceptions

These exceptions are defined in the django.urls module.

Exception Description
Resolver404 This exception is raised when the path passed to resolve() function does not map to a view.
NoReverseMatch This exception is raised when a matching URL in your URLconf cannot be identified based on the parameters supplied.

Django Database Exceptions

These exceptions are defined in the django.db module.

Exception Description
DatabaseError This exception occurs when the database is not available.
IntegrityError This exception occurs when an insertion query executes.
DataError This exception occurs when data related issues come into the database.

Django Http Exceptions

The following exceptions are defined in django.http module.

Exception Description
UnreadablePostError This exception is raised when a user cancels an upload.

Django Transaction Exceptions

These transaction exceptions are defined in the django.db.transaction.

Exception Description
TransactionManagementError This exception is raised for any and all problems related to the database transactions.

Django Exception Example

Suppose, we want to get the employee record where id = 12, our view function will look like below. It raises a DoesNotExist exception if the data is not found. This is Django's built-in exception.

// views.py

def getdata(request):  
    data = Employee.objects.get(id=12)  
    return HttpResponse(data)  

// urls.py


It will show the following exception because no record is available in the database at id 12.


Django Exceptions

We can handle this exception by using try and except.

// Views.py

def getdata(request):  
        data = Employee.objects.get(id=12)  
    except ObjectDoesNotExist:  
        return HttpResponse("Exception: Data not found")  
    return HttpResponse(data);  

Exception:Data not found

No Sidebar ads