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