From the course: Django: Forms

Django form class

From the course: Django: Forms

Start my 1-month free trial

Django form class

- [Instructor] It's been pretty great to create a form for our website. We've seen how we can create labels for the different inputs and that there's different types of inputs, we've learned how to submit that, the thing is, there's a much simpler way of doing this. And the reason that I showed you this, the sort of manual, hard-working way here, is to give you appreciation for how awesome django can make this process. So, let's go ahead and go into our pizza directory here. We're going to create a new file and we're going to call this forms.py. So inside of our forms.py, we need to import some code from django, so we're going to say up at the top here, from django import forms. And then, we're going to create a class that's going to represent the pizza form that we want to create. So we're going to type out class capital Pizza capital Form and this is going to come from lower case forms.Form do our semicolon there and then now with this class, we can specify the fields that we want to be inside of this form. So, if we go back to our order.html, you can see we want to have a topping1, a topping2, and a size. So, let's go ahead and do that. We can say, topping1, we want that to be equal to lower case forms.CharField Great. And then inside of here, we can specify what we want the label to be, so we'll say the label should be equal to capital Topping 1 and we'll go ahead and add a comma here and say the max length on this topping should be equal to 100, we can't have people asking for toppings bigger than that. And we'll go ahead and copy this same thing for topping2. Save ourselves some typing there, just make sure you change the two at the front and the back. And then we'll also do one for size. So we'll say size is going to be equal to forms dot, and before we had used a CharField to represent text, in this situation, we want to have some sort of multiple choice box just like we had before. So, we're going to say capital ChoiceField and then we'll specify that we want the label to be equal to we'll do capital Size and we'll also specify the different options that we want to have. So we'll say choices is equal to and we need to provide a list here so the first item on the list is, we're going to specify, and in parentheses here, we want small, okay, and then we'll do a comma on that, and then we'll provide small again do comma, one half capital Medium, make sure that's in a string there, comma, we'll also do medium. The reason that we're using the same word here is you can have a visual name but also a different name that gets passed via your form if you'd like to specify that, but for us, we're just going to keep each the same to make it simple. Go ahead and enter that in. Great. So, with this all in place, we have this new class called PizzaForm. We can go ahead and move over to our views.py and we're going to return back the request for the order page we're going to go ahead and make a new form and this should be equal to a pizza form. So that means we've got to import it. We'll go up to to the top up here and say from.forms import capital PizzaForm then we'll come down and say form is equal to a new PizzaForm with our parentheses there and let's go ahead and pass this to our template. And we'll say pizzaform is going to be the value of whatever is inside of form. So we'll go ahead and save this and now, this is the real magical part. When we go to our order.html we can take everything, keep your input for submitting and keep your csrf token, but take the rest of this text delete that and simply just put that we want to include our pizzaform that we passed to this template. If we hit save and now go move to our home page reload you can see it's the exact same, we have the exact same form here, right? We can still provide some text here, we can choose different sizes, order that pizza, it's all the same. But look how much more clean and efficient our forms.py is than what we had before. And if we wanted to make a change here, we could do it very simply. Whereas with the order.html if we had to make some sort of change, well, we'd have to, you know, change the label, connect the for and the ids, it could get really, really messy. And so, this is a much simpler and cleaner way of handling this. Django forms are great.

Contents