Handling Multiple Form Classes11:12 with Kenneth Love
Sometimes we need to juggle a couple of different form classes in a single view.
I have to admit, this idea of creating
form_class as non-instantiated references to form classes (that's a lot of jargon, huh?) comes mainly from Django's class-based generic views. We're not quite ready for them yet, but we'll get there.
When we're only working with one model form, our views are pretty simple. 0:00 But what if we have to handle two very similar, related models, and their forms? 0:04 Do we want to create a view for each of them? 0:09 Or do we wanna create one view that handles both forms? 0:11 Let's go to our Workspaces and answer that. 0:14 So we need to handle building something so that we can add questions, 0:17 both our multiple choice questions and our true false questions to a particular quiz. 0:21 So let's start with adding our URLs. 0:27 And then we'll move on to the views, templates, all that kind of stuff. 0:32 So I'm gonna add a new URL. 0:36 I'm just gonna add it down here. 0:39 And, this one is, for the most part, going to be fairly predictable. 0:41 We'll do the quiz_pk, and then we'll say create question. 0:47 You might wanna do, like question add or whatever, but 0:54 you'd wanna make sure that these kind of match, so it's up to you. 0:56 This one might be new for 1:02 a lot of you, if you've not done a whole lot of regular expressions. 1:03 What we're gonna do is MC and then a pipe, and then PF. 1:07 And so that means either MC is here or PF is here. 1:12 And we'll use that to figure out which type of question they want to add. 1:16 Break to new line, views.create_question, name=create-question. 1:22 All right so that means that we're gonna have to create this views create question. 1:30 Let's get to that in just a minute. 1:37 We can put a stub in here if we want just to make sure that we've got something for 1:39 Jingo to find because it doesn't always like it when we just leave things blank. 1:47 This one needs to login_required as well. 1:53 And we're gonna call it create_question. 1:56 And it's gonna take request and 1:59 then we said it would get the quiz_pk and the question type. 2:02 And then let's just do a pass for right now, just to make Django happy. 2:08 So, what we need to do here is, we need to be able to build up 2:13 one of two types of forms, 2:19 depending on whether it's a true false question or a multiple choice question. 2:24 We don't wanna have to figure this out two different times. 2:27 We wanna just figure this out once. 2:30 So before we do that, though, 2:32 I actually want to hop over here to templates and courses. 2:36 And our quiz detail and this is the one where we have like our section 2:43 to edit. 2:48 Let's put in a button that will allow us to add our two new types of quizzes or 2:52 rather a button for each type. 2:58 So url course:create_question. 3:00 Quiz_pk is gonna be step.pk cuz step is the quiz. 3:05 And then question_type is gonna be mc. 3:10 And the reason for that, let's see class=button, 3:16 is because this one's going to create a New Multiple Choice Question. 3:20 So we could say that, and then let's just copy this, 3:25 paste in a new one, and we'll change mc to tf. 3:31 And we'll say New True/False Question. 3:36 All right, so that handles our URL or our template rather, so if we refresh this. 3:43 Sorry I typo'd, that should be courses. 3:53 All right refresh that. 3:58 There we go, so now we have these 2 and none of them are going to anything if we 4:00 go to them, because we don't have a view that does anything there yet. 4:04 So let's go see about adding that view. 4:10 So the view, first all we need to go get the quiz. 4:13 So, let me pull this up just to make sure it's above the closed captions. 4:18 So quiz is gonna be get_object_or_404(models.Quiz, 4:25 pk=quiz_pk). 4:30 So gotta get that quiz and then let's look at question_type. 4:32 So if question type is equal to tf, then our form_class 4:39 is going to be forms.TrueFalseQuestionForm. 4:44 And I'm not going to instantiate it. 4:49 I'm just referring to the class. 4:52 else: form_class = forms.MultipleChoiceQuestionForm. 4:53 All right, so now we have the form class and 5:04 we wanna just go ahead and do form = form_class. 5:09 And now we could have done form equals, and then this [INAUDIBLE] those earlier, 5:13 but where the post comes in makes it to where that's not as convenient. 5:19 So we'll just do if request.method = 'POST:, 5:25 then form equals form class request.POST. 5:30 See we wouldn't of been able to this if we had just substantiated these up here. 5:34 So, we wanna be able to use that multiple times. 5:39 And then if the form is valid, then what do we want to do? 5:43 Well we want to save the question, so question = form.save(commit=False). 5:48 And we wanna do the question.quiz = quiz, that we found earlier. 5:56 And then we wanna do question.save. 6:01 And then I wanna go ahead, I mean, 6:04 this right here is just like what we did up here for creating our quizzes. 6:06 You're gonna do that same kinda thing a whole lot if you have related models. 6:11 And then let's send a message, so messages.success(request, 6:15 "Added question") And 6:22 we'll return our HttpResponseRedirect to the quizzes absolute url. 6:27 Cool. 6:36 And if we're out here and it's not a post then we'll just return, render (request, 6:37 courses question_form and then quiz and form. 6:44 All right so nothing special in this bit. 6:56 This should actually be there we go. 7:02 And our view should be done. 7:07 So the thing that's the most special about this one is this right here. 7:09 We're trying to figure out which type of form we should have. 7:13 And this is really handy when you've got something, or 7:19 maybe you need to edit a couple of types of forms on one page. 7:21 Maybe you wanna have just the ability to create five or 7:25 six different things from one view. 7:29 This is really handy, but we need to go make this template. 7:33 So we'll right click here on courses, choose New File, and 7:37 we'll say question_form.html. 7:41 And so this template is pretty simple, 7:45 it's a lot like our other templates that we've been doing. 7:48 So we'll extend courses, layout.html [SOUND] and 7:52 we'll load course_extras. 7:57 And then in our title block we'll say New question for 8:00 and then we'll use the quiz title. 8:06 And then we'll put a pipe and we'll say quiz.course.title. 8:09 And I suppose you could say new question and 8:16 then you've got the quiz course title but I like it both ways. 8:20 And then block.super and then end that block. 8:24 And then our breadcrumbs block. 8:32 And you can all kinda just skip over this bit if you don't wanna do it, 8:36 because it's just for making the site look nicer. 8:39 So courses:detail will be quiz.course.pk. 8:42 And we'll put in quiz.course.title. 8:49 And then, let's see, I'm just gonna copy and paste that. 8:54 Cuz I don't wanna type all that out every single time. 8:58 Course_pk would be that, and step_pk would be quiz.pk. 9:04 And this would be quiz.title. 9:10 All right, and then we'll end that block. 9:13 And then we'll do our content block. 9:19 Go ahead and end it just so I don't forget. 9:21 And then we wanna do our block.super. 9:34 If you haven't already looked through the courses layout, it's kind of 9:37 a neat use of super, because we define this block and we define this block and 9:41 we're using block.super it puts this block into this block, which is kinda neat. 9:45 And we'll say make a new question. 9:50 And then we need our form, And the action is just here. 9:53 And then we need our csrf. 10:04 And then we need our form.as_p. 10:08 And then we'll do our input, type=submit. 10:15 Class=button, value is equal to Save. 10:18 And that should be it. 10:25 All right, let's go see if this works. 10:28 So I wanna make a new multiple choice question and I've got my prompt and 10:30 I've got the shuffle answers, which is only on multiple choices, and a new true, 10:35 false, doesn't have that. 10:39 Let's just put this in and we'll say strings. 10:41 Are addable like numbers. 10:45 Now you can add strings like numbers. 10:48 And we'll save and let's go look in our admin and 10:53 see if that question got added. 10:58 So if look at true false questions there is our question. 11:05 Awesome, that's great. 11:10
You need to sign up for Treehouse in order to download course files.Sign up