Types of Errors in Django

Types of Errors in Django

While working with Django, we all have faced several types of errors. Let's have a quick look at some most common types of them.

P.S. There are many more types of them and the ones covered here are just the ones that I faced working with it till now. Also, do suggest some other use cases of the mentioned exceptions if any.

ValueError

Generally, a ValueError occurs when the parameters are passed incorrectly/invalid or are missing in a function, or in other words, the function receives an argument of correct type but incorrect value. A simple example can be a numerical value divided by zero passed or the square root of a negative number.

NameError

NameError generally arises when one is trying to reference a variable/identifier that has not been declared yet. It's a kind of runtime error that can simply be handled by exception handling. For instance, the 'user' variable is not declared but accessed or is not correctly accessed/imported from a file where it is declared.

TypeError

This type of exception occurs when one tries to perform operations between incompatible data types like adding a string and an integer or passing an incorrect type to a built-in function e.g. passing a list to the built-in add() function. Calling a non-callable object e.g. calling a string.

FieldError

Field errors are raised when models have some errors. For instance, one or more fields may not exist in a model class or may occur with the same names, invalid use of drop, delete, join, or other keywords, using the same name in a class is correct syntactically but it can be a problem for Django Models. Therefore, the exceptions will check for these kinds of things.

ObjectDoesNotExist

This type of exception occurs when we try to request an object that doesnot exist. It emerges mainly from get() method in views and is the base class of all exceptions.

OperationalError

This type of exception is closely related with the database like no proper migration of tables or missing of one or more columns or duplication of one or more tables/columns. Generally gets solved by manage.py migrate/makemigrations command after creation/deletion of database. Certain commands like sql "appname", syncdb, etc can also be used to check the information of the database.

IntegrityError

This type of exception occurs when two or more entities with the same primary key/ foreign key try to register in the model where the model's relational integrity fails or when post()/get() method is unable to access the input value correctly from the form.

NoReverseMatch

It is one of the most common errors. Generally occurs due to the absence of matching urls(a request for url not defined in urls.py). For instance, /login is not defined means /login doesn't exist or is missing from url-patterns.

PermissionDenied

This error is also quite common and is raised when the directory location of static files is not accessible. This can be easily prevented and handled using try/catch block. Furthermore, in the settings of static files, accessibility should be public and not hidden/protected.

ViewDoesNotExist

Django by default checks for all urls and view functions. If the corresponding view function is missing or is faulty, this type of exception occurs. We can also raise this error when urls-config can’t get the view to load. It is a problem that occurs while using relative URL addressing.

MiddlewareDoesNotExist

Django comes with most of the pre-installed middlewares in settings.py required for our application. Most of them are there for the admin app and all of them are utilized. This type of exception occurs when there exists any unused middleware is present in the middlewares list.