Try DJANGO Tutorial - 27 - Form Validation Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Jago has a lot of built-in validation for its fields so when we render out a Django field or jingle form we actually can submit some data and Django will check to make sure that data matches that built-in validation so like if it's a number in the decimal field for example but there's a lot more things that we can do than just what's built in and that's we're gonna do it in this one [Music] so if we take a look at the model form which is now what we're gonna use I'm gonna show you that it's really simple to override the field themselves right so if I actually bring this title in over here I see that this is the same name as that one so that's actually how you override what comes in by default kind of a cool little feature to have so now what hopefully you realized and I did mention in the last one but now these forms are actually identical they have the same three fields the only thing that's different is how the view handles them that's it but we're going to go back to using that product form itself so I'm gonna go ahead and comment out all of this and we'll come back into our original product create view that we were using okay so what I have here is a shortcut method to what I did up here right so that request post and then render these things out all this does is renders out that form if post data comes through otherwise we'll just render out an empty form that's all that's going on that right there okay cool so now that I understand that what I want to do is just take a look further with this is valid related stuff so let's go ahead and take a look make sure my form is coming through a refresh in here and I've got my price my field was required so it's possible that I might need to save a few things here let's make sure everything's saved up we've got our forms there we go and we refresh our title goes away and if I go back on the create it shows me that I have my placeholder there so yes I could absolutely just copy all of these things in here as well and once I do that I have a more robust form in the sense of how I wanted to look and almost how I wanted to work so so when you do this you realize that you did change quite a bit and yeah there's absolutely shortcuts to doing this there's third-party packages that do a lot of this stuff before us which we're not going to talk about yet because it's getting a little bit too advanced but instead what I'm going to talk about is like what if I want my title to be you know to contain a certain word let's say I wanted my title to contain CF e right so that word itself or that those initials itself what I need to do here is come in and say define clean underscore then my field name so whatever field that I'm gonna be grabbing the validation for this is what I want to do clean title right so I want to make sure my title has a specific item in there so as you say self and we can do ours and keyword ours those are probably not necessary but oftentimes if you're not sure when you override something just make sure you put ours and keyword ours so to get the actual title itself we do self that cleaned data get title okay so this is gonna get the default title this is gonna get the post Django form cleaning so Django will clean the form itself initially and then it's gonna trigger this title in other words this is not overriding required for example that required will absolutely still be in there and to validate this we just are gonna go ahead and say if title or rather if CFE in title then we'll return well do we want to do it this way right so let's let's just do the logic first so if see if he's in the title then we'll return that title whatever that default is otherwise we'll do forms dot or rather raise forms that nation error this is not a valid title okay so we save that and let's take a look refresh in here I'll say ABC hit submit hey this is not a valid title there is our validation it's really really simple I mean it's not that advanced or that complex and of course I actually wouldn't write it in this format what I would do instead is say if not in that title then raise the validation and the default returning that title now the reason for this is so I can have multiple validations if I wanted to if news is not in the title not a valid title right so ABC space we got two not invalid well let's actually save and refresh this let's do that again so I say ABC I hit save I've got this is not a valid title ok so right off the bat it will raise that one now assuming that I put CFE in here like that and hit save again it's gonna say not about a title so it would have to validate all of these conditions that you might have and of course these aren't realistic ones for a title this would probably be more realistic for like an email so let's say for instance if I did clean email and we did email I'll throw in this email in just a second the actual field itself so let's just go ahead and say if not or rather if email ends with the Adu or if not ends with edu then we'll do Ray's form validation error this is not a valid email then we can return that email and since I'm doing this this is a new little feature that you might end up using is you could say email equals two forms dot email field you notice it on the model form it's certainly not on the model it's like a refresh in here I got my email filled in there I say ABC @ gmail.com I hit save not a valid email so it's pretty cool validation is fairly simple and then you can run you know any sorts of validation things on here now I will say that doing validation is not only on a form but since we've been working in these forms it's a really good way to introduce validation as a concept you can absolutely do validation on the fields for a model as well but the idea for validation works whether it's a model form or just a standard django form and what you should see or hopefully this pattern that you're noticing is that if I commented this out and change this to a form the form itself will render the exact same all of the validation will render the same all of that but of course the one caveat is how the actual form would work in the view it is slightly different so if you have any questions on validation please let me know otherwise make sure you subscribe to get everything we're gonna be doing so much more Django stuff and stay with us
Info
Channel: CodingEntrepreneurs
Views: 76,884
Rating: undefined out of 5
Keywords: djangourlshortcfe2018, django 2.0 tutorial, django tutorial, install django on mac, install django with pip, install django with virtualenv, virtualenv, python django, Django Web Framework (Software), Mac OS (Operating System), Python (Software), web application development, learn django, installing django on mac, pip, python package, django, kickstarter funded, beginners tutorial, trydjango2017
Id: wVnQkKf-gHo
Channel Id: undefined
Length: 7min 48sec (468 seconds)
Published: Tue Jul 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.