Learn HTML

Learn CSS

Learn Javascript

Django Introduction

Django Project & File Structure

Django Create & Structure Directory of Application

Django View

Django Template

Django Dynamic Templates Using DLT

Django if,elif,else & for loop tags

Django Static Files

Django Template & Static File Inheritance

Django Hyperlinks

django Get Post Csrf

Django Administration

Django Model class

Django Form

Django Get Form Data & Data Validation

Django Redirect Page After Submit

Django Save,Update & Delete Form in database

Django Dynamic URL

Django User Authentication System

Django get form data and data validation

Django form and field validation

After filling the fields by the value we have to validate those data. Suppose user put an email. In this case validate means, is the user write the correct format of the mail, does the user use "@" symbol or not, etc. After doing the validation you have to put the data in the database or do whatever you want.

In django we don't need to do code for validation because it is already done by django but we have to run that validation.

is_valid():
This method is used to run the validation.If the data are valid then it will return True otherwise False.

Syntax:
formObjectName.is_valid()

cleaned_data:
This is an attribute and this is used to access the clean data. Clean data means, suppose the data given by user is valid, so it is a clean data.Now to access those data this attribute will be used. cleaned_data only contains valid fields in the form of dictionary. In the dictionary fields name become the dictionary key and the inputted value by user become the value of the key. Suppose there is 5 fields. So in the dictionary there will be 5 key value pair.

It means that at first user will fill the fields and the validation will be performed and if the data is valid then you can show those data in terminal.

Syntax:
def viewFunctionForForm(request):
   if request.method=="POST":
      formObjectName=FormClassName(request.POST)
      if fm.is_valid():
         print("Form validate")
         print("Field Name:",formObjectName.cleaned_data["field Name"])
         print("Field Name:",formObjectName.cleaned_data["field Name"])
   else:
      fm=FormClassName()
return render(request,"path/html_file.extension",{"key":formObjectName})

Here if condition says that, if the method of request is POST then create the form object. If the form object is valid then print the form is valid and print the other form. If this condition isn't true then in the else part simply create the form object and render the form object.

Now there can be a question and that is:
How the method of request can be POST?
The answer is, you have to pass method attribute value POST in the form opening tag .




print("FieldName:",formObjectName.cleaned_data["field Name"])
print("FieldName:",formObjectName.cleaned_data["field Name"])

By using these two lines we are printing the value of which is putted by user in each field in the terminal.
If don't want to print values in terminal then don't use these two lines of code.

How to perform custom Clean and validate method for specific field of a form?

To do this one method is used and that is clean_fieldName().

How to perform custom Clean and validate method for all field at once of a form?

To do this one method is used and that is clean_fieldName().

How to match two fields value for validation?

Styling errors of form and form fields

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.