Data Validation with Django Forms

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so now we're going to talk about data validation data validation is what happens when you hit the submit button and then that data is sent to the to the server and what the server does is it checks that post data that it's getting to see if it's proper according to its rules so if you go back to this picture I showed you in the create form the part we're talking about now is really this part right here where the user has entered their data they've hit the submit button and we in the server are going to look at that data and then we're going to decide if it's a an error send back a form or store the data and we're gonna we're actually sort of just have a comment so you store the data and then go to the success so we're really looking at this part right here of receiving and validating the data and then taking all those actions okay so the idea of data errors is you know you're typing in a form and you forget your zip code and you hit the button and it little red box pops up and says zip code is required now you can also tell the fields in the browser that they're required so that the browser won't even send the data that's different and not all browsers do that so you still have to check if you really want the zip code you got to check to make sure the zip code is there now in this one I made it just to generate errors I made a validator on that form if you recall this said that all the titles have to be at least two characters and here's a little message please enter two or more characters if that's not true because otherwise you know that the fact that it's not a string or a number or whatever because the browser is a little too smart for us so I had to come up with a validator that would fail in the server and not fail in the browser you still need to validate data in the in the server so you type some stuff in you make a mistake like in this case the title was only one letter I hit the submit button and now what I want to see is a thing that tells me what the errors are and gives me a chance to fix it including putting the old data that I just posted write it so it gives me my took post data back and so and then I get a chance to to resubmit so again here's the data validate data form and the validator there is - it's a min length of alligator there's if you go to look into a documentation there's a jillion validators I just this was really simple and it fits on a slide so basically in addition to whatever the fact it's got to be a character field I want it to be at least two characters and there that's not an instant a crazy validator to have because you know you might have a post that says look you're not gonna have a title of one character when you're doing a blog post or something like that right you might say look at least five characters so now we're going to walk through that process and so if you take a look at this code from the forum in comes a get request and that get request is going to show up with some old data it's going to create the form with the initial settings it's going to build a context that it's going to pass into the render same render form that just makes the thing with the table tags and CSRF and form post and so at this point we are a get request we are returning a two hundred which is returning the HTML for that page right so that is that's the first part of it and that is the in the Edit process get the old data show it to the user and give the user an opportunity to change the data so now we change the data right and let's just you know say we didn't make any mistakes you can changed a few numbers you hit the post which gathers up all this stuff and sends it as a post request to the server so then what happens as it comes into the post method see how these class-based views are so dang pretty right so pretty and it comes and so request post read all that data coming from the form comes into request post and you just construct the basic form and say hey basic form your old data is from the post and all the fields are named the right way and the key value pairs I mean this is basically a dictionary but the form made the key value pairs it knew what names to name all the forms so it named them and so it's not surprising it knows exactly what to expect in there right and so it takes a look at the post data and then it constructs us a form object right it can trucks us a form object and basically says if there was a validation failure if not form is valid then we're going to do one thing but in this case I didn't make any changes so for me is valid so it drops down this is where we would normally store the data off to the model but we're keeping this really simple and then I'm going to redirect with a 302 to form success the other thing I might do is put a message in the session so that the message makes it into here but I'm going to keep it and keeping it simple and the browser immediately comes back and does a get to slash form slash success and then I return thank you very much I might have pulled a message out as we've seen in an example where I have from the post method to the next get I drop a flash message into something but there's better ways to do that and I'll show you that in a upcoming lecture series and so that's how that's the success flow and it works great now let's take a look at what goes wrong so in this case I've got my form I put in the wrong kind of title it's too short I send a post it gathers up all this data sends it along with the post that comes in and request dot post and so now we the form comes back and now form is valid as false because I've violated a rule now the interesting thing is in this form object is not only the old data but also all the error messages because at this moment it is constructed a form object and part of the form object is error messages okay so if it's not valid let's make a little context okay let's make a little context and pass that in and then out out it comes and and at that point that formats table is going to print out all of this stuff all those error messages come back so I render it back now I render it back and then of course you can submit it and maybe get to a success now there is one sort of interest question about this page which I have asked a stackoverflow question about and I'm not sure I'm ecstatically happy about it and that is this pattern is exactly how Django wants you to write it it wants you to write this and if you'll notice I'm doing something I told you not to do Django is telling me to do something that I don't want to do and that is return a 200 at the result of a post so you got a post now the key thing is that the thing that everyone will agree on is once you store data you have to return a redirect and so the Django people who've done this for years they consider it okay to return the 200 and a page after a post if no data was changed now the problem is if you hit refresh on this you're going to get the weird funky message but this is how Django works and so I don't know what to tell you I can write my forms differently it's a few more lines of code in here and I've written that and I've made it so that I passed the error message and I serialize the form and put it in the session and do something but I'm not gonna fight City Hall on that one I'm just gonna say if Django thinks it's fine is the thing we do agree on is if you're gonna return it 200 don't return it 200 after you've saved at it because then you'll double save the data like you'll double commit a transaction you'll double do a transfer you'll double order it'll do whatever but in this case all we did is looked at the data came back and then sent back an error message so you can look at the Stack Overflow where I sort of argued for a little bit so it's been a long lecture series we talked about HTML we talked about HTTP how getting post or different ways of doing it we've talked about the idea of you can't how to fix the Refresh we talked about cross Cypress cross-site request forgery CSRF and how jingle handles CSRF and then toward the end here we've talked about how to define forms in Django to sort of outsource all that HTML generation how we do validation now there's much more to learn on this and we are going to learn more of the features of form objects as we go forward and build increasingly complex applications [Music] [Applause] [Music]
Info
Channel: Chuck Severance
Views: 10,598
Rating: undefined out of 5
Keywords:
Id: kRXiMIFKzoQ
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Wed Oct 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.