Django Form Errors
Today’s topic is very important to learn because it needs when you want to make a Django form advanced level. Because Django Form Errors are displayed when users enter invalid data in the form input fields. So, each and everything we will discuss related to Form errors and we have discussed others Django form topics on this blog platform. You can read that articles easily, which we have added at the bottom of this article.
The form is the collection of different fields in which the user gives his/her details to send data to the server for performing some reaction. Such a form can be developed with HTML markup language. But if you want to make an attractive and beautiful Form, then make use of CSS and Bootstrap. But we are talking about the Django web framework form which has a built-in class form, just we have to call that class to use Django form functionality. We did not have to learn how to create Django form but we are discussing Django form errors and how to use that error to show users.
What means Django Form Errors?
When a user fills the form, it has different fields having different types of data that the field accepts to store in the database. For example, a field in which you have to enter your name in text type, but you cannot use numbers or special characters in that field. If you enter numbers or special characters instead of alphabetical-based text, then you will get a Django form error.
Â
There are different Django form error messages below the form field or top of the form. There may be different types of form messages it depends upon you, which kind of message you want to display to the user. But some messages are predefined in Django, that you have to use but you can modify those messages.
Â
What is is_valid()?
It is the function that is written after the display form, it checks wheater the form is valid or not. What means valid or invalid. At first, we check wheater the user gives proper and accurate data to input boxes as required or not.
Â
If the user gives improper data or input that a field disallows then due to that field as it has improper data, the form will not process to further steps. Because there is some issue with one field of form due to that your form will not process further.
Â
I am saying about is_valid(). Because it is the function that checks the data validity or data accuracy. We make a condition using an if-else statement. We check if the form is valid then do this function if the form is invalid then execute statements in the else block. So, when your form is invalid then statements of else block will run.
Â
All the Django form errors will display when form.is_valid() is run. After this function, all the Django form error messages will display to the user giving instructions user.
Â
In the coding example, you will learn more about Django form errors. Each line has a comment, you can understand, which line, which means.
Â
def jafricodeview(request):
    if request.method == “POST”:
        form = CreateUserForm(request.POST)
        form.is_valid():
        form.save()
    else:
        form = CreateUserForm()
        print(form.errors) # see below the paragraph its description
    context = {‘form’:form} # we passing dictionary data to template
As we used form.errors to display error. It will dispaly error when form’s fields have some issue. Isssue means when user enter invalid data.