Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating a Form4:04 with Kenneth Love
Creating a form in Django is no more difficult than creating a model. Easier, even, since you don't have to do any migrations!
Creating a form in Django is no harder than creating a model. 0:00 In fact, they have very similar APIs. 0:04 So, a lot of those may seem familiar to you. 0:06 All right, so I wanna talk about forms, but 0:08 I mentioned that the site looks a little nicer. 0:10 So, I wanted to show you that real quick. 0:13 You can see that i'm in the learning_site directory. 0:15 And as usual, I'm gonna do python manage.py runserver. 0:17 All those zeros, and then 8000. 0:22 Well this is what it looks like, 0:27 I can refresh it just to make sure this is what it looks like. 0:29 But here you go, we've got our welcome that we had before. 0:33 We've got a little Courses drop down here. 0:37 And if we go look at the Courses we can go look at a course. 0:41 Everything looks pretty much just like it did, 0:44 it's just a little bit nicer looking now. 0:45 Couple other things added in. 0:47 Feel free to look through the templates and 0:48 the template tags to see what all has changed. 0:51 But let's start talking about forms. 0:53 Now if I wanna make a form for our general site, 0:56 which is really what a contact form or a suggestion form would be. 1:00 I'm gonna put that into our learning_site stub here in our learning_site directory. 1:06 And so I'm just going to make a new file. 1:12 And since this is going to hold forms, I'm going to call it, here we go. 1:15 Gotta type in the right spot. 1:19 I'm gonna call it forms.py. 1:21 And inside forms.py, I need to have access to all of Django's form library. 1:23 So I need to have the ability to make a form, I need to have all the widgets and 1:29 the inputs and the validators, all that kind of stuff. 1:34 So, to get all of that, I'm gonna say, from django import forms. 1:37 And I have to be able to spell it correctly. 1:43 And just like with our models, forms are a class. 1:46 So we're gonna make a new class for our suggestion form. 1:50 So I'm gonna call it SuggestionForm cuz I like to be explicit. 1:54 And forms.Form. 1:57 And there we go. 2:00 Okay, so, all this does is this gives us some basic stuff. 2:02 The form will know how to clean itself. 2:06 The form will know how to turn itself into HTML. 2:07 Or whatever we need it to do. 2:10 So, let's add some fields to this. 2:14 Let's go really basic for the beginning. 2:16 This is just a suggestion form so let's get someone's name. 2:17 And we wanna put in a CharField, which that's, again, 2:21 just like it was for models. 2:26 I want their email address. 2:28 So EmailField, again just like it is for models. 2:31 And let's do a suggestion. 2:35 This one's a little bit different. 2:37 This is where I want them to type in their suggestion. 2:38 And really as far as the form goes, is just going to be text, right? 2:41 It's just a CharField. 2:48 But, when I present this as HTML, I want it to be a large text area. 2:49 So I need to specify what the widget is. 2:55 And the widget is just how the thing is represented to HTML. 2:57 So, we specify a widget and then we specify what the widget is. 3:05 And this is gonna be forms.Textarea. 3:09 And as always, when we're creating something inside of this kind of structure 3:12 with forms or models, we don't want to call it, we don't want that. 3:17 We just want a reference to the class that it belongs to. 3:22 That gives us our form. 3:26 I know I keep bringing this up, 3:29 I wanna actually just show a side-by-side comparison. 3:30 Let's look at our models here. 3:33 And we've got CharField and TextField. 3:35 And I mean, that's really about it. 3:38 We've got an IntegerField. 3:42 The API, the way that you use the forms is almost identical to the way 3:43 that you use the models. 3:48 So its really nice that Django has that consistency. 3:50 All right, so there's our form. 3:53 We have three fields and even a custom widget. 3:55 We'll add some custom validation to the form later, 3:58 but I think right now we should get on with serving this form up as HTML. 4:00
You need to sign up for Treehouse in order to download course files.Sign up