Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Model Form for Answer10:04 with Kenneth Love
One last model to set up a form and views for. Let's get some Answers!
We have a pretty good handle on creating and editing our quizzes and 0:00 questions, but we're really short on creating answers. 0:03 Let's go make a view and form for answers. 0:06 So, I wanna be able to add an answer to a question. 0:09 So for instance, right now we have this question, 0:13 what all characters can be in a string? 0:15 Be nice to be able to add an answer to this. 0:17 So, let's see about doing that. 0:19 So, let's start here on our question form and 0:23 in this div here let's do, if user.is_authenticated, 0:28 and then we'll end our if. 0:35 Let's throw in an <hr>, just to put a little bit of separation there. 0:41 And then a href, for right now, I guess, "#" class=button. 0:46 And we'll say, Add Answer. 0:53 All right, so, that's cool. 0:58 We've got a way to add the answers. 1:00 Let's go refresh this and make sure it shows up. 1:02 There we go, Add Answer. 1:05 All right, and so, we should go about figuring out the rest of this. 1:07 First thing we need is we're gonna need a form, obviously. 1:12 So let's go down here and make a new class, and 1:17 we'll call it AnswerForm and (forms.ModelForm). 1:22 I mean, you should all have this pretty much 1:28 figured out by now, model=models.answer. 1:33 Fields equals, and let's just put in order and text and correct. 1:38 I think that if we look at our models, 1:46 we'll see that those are, yep, order text and correct. 1:49 So, those are the things that we wanna have. 1:54 All right ,so we'll save that. 1:56 So there we go, we've got our form and we've got a link. 1:58 Let's go make a URL for that. 2:02 So, down here somewhere And 2:07 we'll do [SOUND], and we'll have a question_pk, 2:12 cuz we need to know which question the answer belongs to. 2:17 And then, we'll say create_answer. 2:23 And that's it, because we're just going straight to creating the answer. 2:28 If you wanna make a edit answer whatever, 2:34 then obviously that's the thing you're gonna bring in here. 2:37 Views.answer_form and name=' create_answer'. 2:42 All right, so. 2:48 Yeah, all right. 2:51 So then, we can go change this, our URL, 2:53 to be url 'courses:add_answer", right. 2:57 Or was it create_answer, it's create_answer. 3:01 And then, we'll say question_pk =. 3:08 And that will be Our form.instance.pk. 3:13 So, that means we probably wanna do, if user.is_authenticated and 3:22 form.instance.pk. 3:27 So, that way we know the user is authenticated. 3:29 So in our case, it's a super user or it's someone who can log in to admin, at least. 3:31 And here we know that we're working on an instance, because we don't wanna try and 3:36 add answers to a question that doesn't exist yet. 3:40 So, cool. 3:44 There's that. Let's go back and 3:46 make sure this button still shows up. 3:47 Oh, that's right. 3:52 We don't have our view answer form yet. 3:53 So, @login_required. 3:58 Answer form, it'll take request. 4:01 And question_pk. 4:04 And we'll just do pass for now and refresh. 4:06 All right, cool. 4:12 And there's our Add Answer. 4:13 And that shows up because we have saved this. 4:13 Looks like our URL's a little weird, though. 4:17 [SOUND] Oh yeah, because we forgot to close that. 4:20 Hopefully, some of you didn't make that same mistake that I did. 4:26 There we go. 4:29 That's a URL that looks like we want. 4:30 All right, great. 4:32 So, we've got our link in the template, we've got our URL, we've got our form. 4:33 So, let's go see about making our view. 4:38 And so right now, we're not doing this to where we can edit anything, 4:44 you can add that in later if you want. 4:49 Let's start with getting the question, so 4:52 get_object_or_404(models.Question, pk is going to be question_pk). 4:55 All right, so that's our question. 5:04 And we, of course, we would wanna do form=forms.AnswerForm(). 5:07 If request.method is equal to 'POST':, 5:13 if form., I almost forgot step. 5:19 Oh, no. 5:23 And here, we're gonna do form=forms.AnswerForm(request.POST). 5:25 Fill in the stuff that people gave us. 5:33 Then if that form is valid, then we wanna 5:36 do answer=form.save(commit=False), 5:40 answer.question=question, answer.save. 5:46 And then this is where we'd wanna do like, a message, 5:51 so messages.success(request, "Answer added"). 5:56 I mean, we're basically recreating what we did right up here. 6:06 All right. 6:10 And then we'd wanna do return 6:13 HttpResponseRedirect reverse, actually do our 6:16 So, our questions have an absolute URL, so let's just do that. 6:27 (question.get_absolute_url()). 6:33 All right. And if we're not in a post, 6:39 then we just want to do return render(request, 6:42 "courses/answer_form.html" and 6:47 let's send out 'question':, ask question. 6:51 Let's put this on a new line, that's getting too long for me. 6:56 And 'form': form. 7:00 All right. 7:09 So now we just gonna make that answer form. 7:10 So new file, answer_form.html. 7:14 And I have a feeling on answer_form, 7:19 we can copy a lot of stuff here from question_form. 7:22 Let's copy and paste, and we'll do some editing. 7:25 Obviously, we don't need this add answer thing, because, well, 7:31 there's not going to be an add answer on a question. 7:35 And so then, this would probably just be new answer. 7:39 And here, we'd wanna say question.prompt. 7:48 And then question.quiz.title, and block super is fine. 7:53 And this would be question.quiz, question.quiz.course.title. 8:00 This one, we wanna go back to the quiz. 8:09 So, quiz. 8:11 Yeah, question.quiz. 8:17 Question.quiz. 8:20 And then, we'll just put in an li here for 8:24 the question.prompt. 8:29 And we won't have a link back to the question, but 8:33 that way we can see what the prompt is while we're editing stuff here. 8:35 So we had the block super, and then here we'll 8:39 just say Add Answer, and then there is our form. 8:44 All right, so let's go see if this works. 8:49 Let's hit Add Answer, and 8:52 here's our Order, here's our Text, and oh, we don't have this thing. 8:54 Oh, that should be question.quiz.title. 9:02 Haha, there we go. 9:06 Those breadcrumbs are nice and handy. 9:08 So, what all characters can be in a string? 9:10 Let's just add in ASCII only. 9:11 And here's our answer coming out. 9:16 And let's add in, let's go edit that question again, hit Add Answer. 9:21 So, this isn't the most convenient thing, is it? 9:26 And then let's say All of Unicode. 9:30 And that one should be correct. 9:36 We hit Save. 9:39 So cool, we've got both of those showing up. 9:40 That's not the best solution, is it? 9:44 I mean, it works. 9:46 We go to the add answer view every time we want to add an answer. 9:48 But that means we have to do a lot of thinking about our answers before, during, 9:52 and even after creation. 9:55 Not how I wanna spend my days. 9:58 Maybe we can find a way to create multiple answers at once? 9:59 Let's tackle that in the next stage. 10:03
You need to sign up for Treehouse in order to download course files.Sign up