Model Form | Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I saw this question in the comment section and I thought this would be a perfect opportunity to make a django models form tutorial so in this tutorial we're gonna create a jingle model form which basically grabs a django model a database object and creates a form based on all the attributes so we don't have to manually render out in HTML form we're just gonna create that form object and throw it into the template and it'll create everything for us and then we're actually gonna save that form and show you how that form actually processes an item and adds it to the database so I set up a base django application here i created a one model just this customer with the first and a last name and i have this template with a form but it only has it only has that input field that's the main button and i have this view that renders out that template so at this point it just gives us this very blank template so the first thing we're gonna do here is we're gonna follow the django documentation to create a model form so we're gonna follow this method here but before we do that we're going to put our forms into their own file so we can have multiple forms for multiple objects so for this i'm going to create a forms py file and I'm just gonna run the imports right here so I'm going to go ahead and copy and paste these and make sure that this is all cleared up so this is what we need in that forms that py file except for we're gonna be using our model and we're gonna import the customer model here so our models up py file and now we can create that form so we're just going to call ours customer form and I believe you need to throw in model form and this this form right here will take in two attributes it's going to take the model that we want to represent so the model is gonna be our customer model and the fields that we want to out output so at this point we just want to output all the fields that it has so the first and last name so we're just gonna say fields and just do underscore underscore all and then underscore underscore and that should create our form now so at this point we need to throw our form into into the views file and this import customer form so we need to throw our form in here and then we need to render it out in the template so we're just gonna set the form variable and that's going to be customer form and we just throw that into the template like this right here and context is passed in right here and now that we have it in here and we're passing it in to the template we can now actually use it in the template and we just simply use that Jinja method right there and say form so it represents our customer form at this point and it should be as easy as that and there we go we have our form so these are the two attributes we specified and now we want to actually save this form so I at this point action sends us to Wendy show to show you the URLs file this current template gets represented or it gets output by that default and that homepage URL so we're just going to send it back to that URL so action is to that base URL and the method we're gonna use is post so this is gonna be a post method because we're sending data and we want to create the object so the first thing we want to do is throw on a CRS F token C or actually need to remember how that was see s RF underscore token and this basically just lets jingo know that we can accept this this is a safe object to take in where it won't accept it without it and in here we want to write a little conditional so we want to say if request method is post so currently it's a get method but once it's a post method that means we have some form data to process so we want to change this up and we want to throw that in again and we just want to and I'll actually console out the request method before I do anything else so or not console but print so I'll show you the data that the form has given us first before we actually process this so at this point if I just go put my first and last name in here okay so something is not right with that token oh and this needs to be a % there that's why analysis let's just refresh this and test it okay so that sends us that token the first and last name so to process this all we need to do is throw in that post method into the form and because we're throwing it into the form it already grabs the attributes that Django provides for us with that and we just say request dot post and now we want to save that for him so we say if form dot is and we're gonna use valid method so if the form is valid go ahead and save that for him form dot save and I'll just separate that a little bit to make it cleaner so if the request is post throw in the data and save the form check if it's valid if all the attributes are correct and save it so now we can run that one more time and we're gonna process that and I'll just go to my admin panel and see if that item was added so there we go the customer is added and the siphon off and that's simple that's how easy it is to create a django model form and actually add that item into the database and also render that out in the template and i hope you found this video helpful thank you for watching
Info
Channel: Dennis Ivy
Views: 128,810
Rating: undefined out of 5
Keywords: DJango, Forms, Model, Model Form, Dennis Ivy, Dennis Ivanov
Id: VOddmV4Xl1g
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Fri Nov 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.