custom validator in django | How to Add Custom Form Field Validation |Django tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome back to Django tutorial in this video I am going to explain built-in value data and custom validator till this point I have created a form but not validated with the proper input now time to validate the form using inbuilt and user defined way so here I explain the complete process how to validate the form for the coding so let us start the discussion through coding first of all you need to create a project so type in a terminal Django iPhone admin start project name of the project so here I give the name of project is custom validator once the project is created go inside the folder of the project that is cost validator and then create one application okay application type a command python manage.by start app and name of the application so just I write the name of application is coder validator so Django need to install this application under the setting.pify so go under the project folder open the setting.pi file and scroll down till the install app and then few inward labs are already there under the install app now we need to add a custom application so name of the custom application is code so that application I am going to add enter the project now Django realize that this application is get installed under the project now create a new folder template under the project [Music] under the template we can create the application folder that is a coder and under this coder application we can create the different different HTML Pages now here I'll create one of the file that is index.html now the template is also set under the project now we need to register this template under the project so go to the setting.pr file again and first of all import the OS first I'll import the package OS and now register this template register the template go to the template tab and then go under the directory under the directory list we can type OS dot path dot join and then we can join the directory after the best direct so name of the directory as a template foreign under the setting.pify after that we need to create a table which consists of certain attribute that will render into the HTML page or whatever the data users submit on HTML page to store on the model so go to the application under the application open the models.pophi and create the model now I create a class folder model and send that Model start Ed I'll take some attributes under this model so I am right one of the model attributes name Model start data type of that attribute as let's say character print we can set a certain size let's say I want to set devices to that attribute that is a name then I'll write the designation of my coder at this junior senior likewise again I use the character field and if I want I can mention the size of this attribute that is the max length well again I'm looking master in this Shopify for this address I'm also looking the department of that students I am going to create the third attribute department and again I am going to create the data type of that attributes character going max length I'm using same length that is 55. also I am looking the email of my loaders whoa let's say modern start I'm using email it's fine using the email field for this model now once this particular model is get created we need to add this model in our database so we need to execute a certain command so again we have to type command python manage dot py make migration you can convert our model into the SQL query once the models get converted into SQL query then we can Micron this model into the database we need to type a command python manage dot Divine mind grid [Music] so that all the databases you have migrated into the SQL so this model is successfully added into that project now we need to create a form so to create a form we need to add one of the file under the application under coder application I am going to add a new file that is form stop py now I'll create a form for us feel free to form we need to import the package form from teach and go they are going to import deformed package now I'll create the form so to create a form you can write a class name of your form is let's say quarter form Al form s dot path and then what are the attribute you want to list it onto the form that you can use over here already we created four attribute under the model so same attribute I am going to use for this form so name is my first attribute that I'm going to use some name is equal to bombs dot character for you we can mention the size of this and three neutrals here I'll keep it empty then I'm going to use the designation second Institute on the phone a form star let's say it's again character filled then Department bombs dot again I'm using the same data type for this department and I am going to choose one more attribute that is email forms dot email field so now these four attributes are created under the OnStar py the form is also created under the project now next task is to write the business Logic for this application write a business logic we need to go to the views.py file and now we have to use the functions and method to perform a certain operation first of all under this view.pun we need to import my phone so from dot forms I am going to import the form you know that form is order form this form will get imported now I write a function to display the data so folder details [Music] is my function and parameters request and now I write a certain logic over here for this function which if my request [Music] dot method is equal to equal to post we know that there are two method one is post another is kit if I posting a certain data through the form then this condition will get true so if my request dot method is equal to equal to post I'll create the object that is FM is equal to what are the data under that coder form it should be picked so request dot whatever the data is post request start post also we are going to check if that object is a valid if it is valid then you can take that name is equal to object dot clean data method name clean data and Sony so that will be stored under this and then variable similarly for designation and Des and I am going to use let's say FM dot clean data and again the attribute name that is the designation similarly for cloud attribute is let's say Department is equal to m dot clean data Department over this variable Department information and last one is email so here I use only em that is email so FM is object link data I'm going to access the email in this video if I want I can print this information after that if this valid this is going to happen else if this is so if this is not true means if the request is not post if the request is not post then opposing to the else part now under the else part we are going to just simply write FM as equal to orders details sorry coder form information admin and then finally we are going to return renders requests and the template name so template name is index so application name is and my HTML page is index dot HTML and what are the data it will return I'll store that data into the dictionary oh let npl create a variable form and this object data will be stored into the FM so the business logic is complete once the viewer file is complete now I'll write a search input under the HTML file so go to the template folder and open that HTML file so index.html initially it is empty so I'll add certain HTML tag inside it to HTML under the HTML I am going to write let's say body tag under the body tag I'm just going to use that form that I want to design form action perform is empty and I'm going to use my third is equal to post under the post method inside the form we are going to write the dsrf token I already discussed about this csrf token so will not aware of it watch my previous video so that you can understand what exactly the csrf token purpose of this CS2 inside that I want to render the content of form Palm dot I want asparagraph then we are going to take certain input also that once the user will submit that data is get posted so input is let's say input type is submit and the value is also submit so we need to close this input the swing HTML now we need to write the URLs for application and project now to save the URL you need to go to the project folder open the url.py so inside you're in a p Wi-Fi first of all we have to info the application URL so under the URLs you can find a function name enclosed and then we can say d path to the application I'll add one more path if this part is empty and let it be I am going to use the include function and folder dot URLs I am pointing from this project you one now this URL file is not exist under the application so open the application folder and create that new file that is it your warrants Dot py and copy the content of main project URL instructor event and paste into the application URL remove some of the content from that file so this commented part and remove first I don't want to include any application under the application so I will remove this include function and I am not looking on the admin section from this application as well so I'm going to remove this I didn't even bought that now my task is to add the functional names for the file now under this url.py I need to import The View functions from name of application is let's say coder we need to import the views and you can write the name of you let's say I am going to use index View and also call the function of the muse.py low views Dot name of the function so what are the function you write under the Google Doc UI we can just access that function that is code edited so if you open that tool dot ufi you can find name of the function is name of the function is order details so in application URLs you can mention the same name of the function if you want you can mention the the name of URL as well if you want otherwise you can as it is this part so now E1 has also been set under the application and project URL now first of all I'll execute this project to test the functionality whether till this point get execute all the functionality correctly or not unless [Music] to run a project we need to type a command python management [Music] I will get the warrant copy this URL and paste into the browser now we have to attend the index part so I just type the index now you can find this name designation Department email has been displayed properly but still it is not validated by using either custom validation or in build validation so now we'll try both the things now we'll try both the validation through the Kodi so again back to the coding now I'll make the modification and watch this part carefully that's how to perform the validation under the application so we already created a forms which consists of these four different parts name designation department and imp now we need to import one of the package which handles the validation prom PT dot core import validators this particular package handle all the validators under the Django suppose I want to validate name designation department and email so now we'll use that method optic region so under the name attribute we have mentioned character field and now we are going to validate the things so validators is equal to list of validator we can handle here validators dot we can mention a different different validation suppose I want to mention the max length mean length or other parameters also we can handle this using this Max plan editor here I need to mention the name suppose if I'm using hs6 character I am going to handle that is the max length is 6. if the user enter more than 6 character then it won't be allow to user enter that in so this part I am going to just preserve it this part I'm going to just save it and I'll just again run the project and I am trying to submit the data with this validation now the project is run successfully go back to the browser and refresh the page and enter the details suppose I am enter some more characters instead of 6 here I just mentioned the sum here I entered some valid information and I am trying to submit the information it ensure that this value is most 6 character but it contains 14 karat so this validation is automatically checked so I'll go back to my functionality so here we write validators max length validator 6. so user cannot enter more than 6K if user trying to enter it and get validated through this inbuilt validator what we can say in build a package in their validator do that we can handle this validation now second part is custom validation so without using this package or without using this embed method also we can handle this validations now we'll try the custom validation to perform the custom validation so what I'll write I here write a certain method first collected by prior ND method I want a name [Music] start with character G and write a method which start the name so particular value I'm going to pass whether that value start with the G or not as an input and we are going to check if if the value of 0th character is not equal to Z then it will raise the validation so what particular error that we want to raise we can write over here you can raise inform validation [Music] punch validation area validation era and we can mention the particular message the name taught with G only so likewise we can mention the error in the function so here really a summer edition variety of function name starts with G so let it be we need to make a certain modification so I am not going to use this in build validators so I'll just remove that max length validator compute now validator is equal to inside in this list I am going to write the name of function so my name of function is start with [Music] the name of the function should start with so now that validators will handle this particular function so just save the code and again go back to the browser now we enter the information let's say I into Google department is it's hot and let's remove local advert email with only any kind of error because name starts with j a force I enter any other information let's say probing I'll keep all the information as it is and I'll try to submit it [Music] so likewise we can handle the custom validation and the building related test using B Jambo hope you understand all the content till you find it out you can write in the comment section so I'll be more happy to show you now thanks for watching this video
Info
Channel: Gokul Patil
Views: 643
Rating: undefined out of 5
Keywords: Gokul Patil, python, python interview question, data structure, project development, django custom validation, django custom form validation, django form validation email, django form validation example, django form cleaned_data, django custom form validation error, django cleaned_data.get, django custom form validation example, django, django form custom clean, django form clean method, django form clean, django form clean_filename, django cleaned_data, django clean
Id: dy0hzhYoNrg
Channel Id: undefined
Length: 19min 59sec (1199 seconds)
Published: Wed Aug 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.