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
Now that we've made a model form, let's see how to display and save them.
Did you notice that commit=False
? That's an amazingly handy tool with Django's model forms that allows us to get an instance of a model back. We can then use that instance to add whatever other information we need to the instance before we finally write it to the database (if we even do!).
Remember this tool because you'll use it pretty often.
-
0:00
Using a ModelForm, at least to display a blank form,
-
0:03
is exactly the same work as using a regular form.
-
0:06
You instantiate it and send it to the template.
-
0:09
You even render it to HTML in the same way.
-
0:12
Where they definitely deviate, though, is in the processing of them.
-
0:16
You still use the normal isValid method, but after that,
-
0:18
you can save instances to the database.
-
0:20
This sounds like something we should take to workspaces.
-
0:24
All right, so I want to make a couple of views for
-
0:28
dealing with creating quizzes and editing quizzes.
-
0:32
And I know that I want these to be something that only logged
-
0:37
in users can do, so we need to import a little thingy here.
-
0:41
And so what we wanna do is.
-
0:47
We probably wanna give back some messages at some point.
-
0:50
I'm gonna import this just in case.
-
0:51
Maybe we use it, maybe we don't, but for right now I'm gonna import it.
-
0:55
And then django.contrib.auth.decorators
-
1:00
import login_required, which this decorator
-
1:06
lets us mark a view as requiring a logged in user.
-
1:12
If you've done the flask courses, we've done something like this.
-
1:17
Basically it just makes it so, you log into the admin, and
-
1:20
then you can go see our quiz creation, quiz editing thing.
-
1:25
You can't do it unless you are doing that.
-
1:30
And then from django.http import HttpResponseRedirect.
-
1:35
You know we're gonna need that, because we gotta be able to send people back and
-
1:39
forth to places.
-
1:40
And then, we're gonna need forms, so from.import forms.
-
1:46
All right, let's get started with our views, and I'm gonna bump
-
1:51
this up a little bit just so that I know this is above the closed captioning.
-
1:57
All right, cool.
-
1:58
So first let's make a view for creating a quiz.
-
2:04
So first of all we'll mark it as login_required and
-
2:08
then let's call this quiz_create, pretty explanatory title there.
-
2:13
It's gonna take the request and we wanna get the course_pk.
-
2:16
We wanna know what course this quiz belongs to.
-
2:20
And so of course first we wanna get the course that it belongs to.
-
2:25
So we'll do a get_object_or_440, you should remember this from Django basics,
-
2:32
models.Course and the pk of that should be the course_pk.
-
2:37
And then just like with our suggestion view,
-
2:41
we wanna make a form that's just blank, so forms.QuizForm.
-
2:46
So there's just nothing in there, so cool, simple enough, right?
-
2:51
So if request.method is equal to POST,
-
2:56
so somebody has posted our form, then we wanna do a new version of the form.
-
3:05
And we wanna say request.POST.
-
3:09
I gotta spell it and capitalize it correctly.
-
3:12
And now if that new form is valid, then we're gonna do,
-
3:17
this is a little bit different.
-
3:21
We're gonna make a new variable, called quiz, and we're gonna say form.save.
-
3:25
So normally, if we were to just close this right there, it just saves a new
-
3:30
instance of the quiz in the database, but we're missing something.
-
3:34
We don't have the quiz associated with the course.
-
3:38
So, we need to do that.
-
3:40
So what we're gonna do is we're gonna do form.save and we're say commit=False.
-
3:45
I kinda wish this was named a little differently but
-
3:48
I don't know what I would name it.
-
3:49
But commit normally is true, commit=False means
-
3:53
don't actually put this in the database, just make the model instance for me.
-
3:58
So make the model, hold to it in memory, don't actually do anything with it.
-
4:03
Let me modify some stuff and then we'll save it directly in a moment.
-
4:07
So now we have quiz which is just a quiz instance.
-
4:10
So let's do quiz.course is equal to the course that we
-
4:14
got up at the top there and then let's do quiz.save.
-
4:19
And you know what for, let's just do messages.add_message,
-
4:26
request, message.SUCCESS.
-
4:30
And we'll just say, Quiz added!
-
4:36
I'm a big fan of exclamation marks if you hadn't noticed.
-
4:40
And then we'll return HttpResponseRedirect and
-
4:44
we just wanna go back to the quiz, get_absolute_url.
-
4:49
We don't need reverse at this point.
-
4:53
All right, and so, out here,
-
4:55
if it's not a post, you can do else, if you want, but you don't have to.
-
5:00
Out here, we're just gonna do return render (request,
-
5:07
'courses/quiz_form.html', and
-
5:12
{'form': form}).
-
5:15
All right, so let's go make our template and our URL,
-
5:20
just to make sure we got everything done correctly.
-
5:24
Let's go do URLs first.
-
5:27
So if we come over here to our URLs, and
-
5:30
we wanna put in an edit quiz one, or create quiz one, sorry.
-
5:35
So, URL and let's see,
-
5:39
r and then ?P, there we go,
-
5:44
course_pk would be \d+ just like we had before,
-
5:50
and then we'll say create_quiz and
-
5:54
this will go to views.quiz_create, and we'll give it a name of create_quiz.
-
6:01
Easy enough.
-
6:03
All right, and then for templates inside courses here,
-
6:07
we said we were gonna call it quiz_form.
-
6:10
Ideally, we'll get to use the same template for
-
6:14
both creating a quiz and editing a quiz.
-
6:18
So let's see what that should look like.
-
6:21
So this should be extends course/layout.html.
-
6:31
And then we should have block content,
-
6:36
div class="row".
-
6:40
And then inside of there, we wanna do this.
-
6:43
And let's just say, Make a new quiz, and
-
6:48
then form method="POST" and action= nothing.
-
6:54
And then inside there let's do our
-
6:59
csrf_token and then our form.as_p,
-
7:04
and then our input type="submit"
-
7:09
class="button" value="Save".
-
7:14
All right, cool.
-
7:16
And then we need to end our block.
-
7:20
And I think there's actually something, if we come look at text_detail, yeah,
-
7:28
we've got a couple of extra things here, let's bring those in.
-
7:34
This is stuff that's just inside the templates,
-
7:36
I'll let you look through all this if you want.
-
7:40
Let's send that quiz back out.
-
7:43
[SOUND] Or, sorry, not the quiz, the course.
-
7:49
All right, there we go.
-
7:50
So then here, we can say
-
7:55
New Quiz cuz for now it is.
-
8:02
And we can say course.title and block.super.
-
8:07
And that right there, that will let us have a link back to the course.
-
8:16
And it looked like this was supposed to be columns.
-
8:20
And, if I remember correctly, we need to do block.super right here.
-
8:25
All of this is just design details,
-
8:27
you don't have to worry about doing all of this stuff.
-
8:29
So let's add here, on this course detail, let's add a link for adding a new quiz.
-
8:37
So if we come over here and we look at our course detail,
-
8:42
down here at the bottom, let's do if user.is_authenticated
-
8:48
which just checks to make sure that someone is logged in.
-
8:53
And then we'll end our if.
-
8:57
Let's do an hr and then a href= url 'courses:create_quiz',
-
9:06
course_pk would equal step.,
-
9:10
actually I think we have, yeah,
-
9:15
we have course, course.pk, course.id.
-
9:20
And then let's say class="button".
-
9:25
And let's just say New Quiz.
-
9:30
All right, so if we refresh this, cool,
-
9:33
there's our new quiz button, let's click new quiz yep.
-
9:42
Courses.
-
9:45
Cool, bam, make a new quiz, and there's our thing.
-
9:49
Awesome, we should be able to send this through and make a new quiz, so
-
9:54
let's make a new quiz, and we'll just say Review: Strings.
-
9:59
Let's talk about strings.
-
10:03
And it's gonna have five questions, and the order is gonna be three.
-
10:10
Hit Save, and we get to see the Review: Strings.
-
10:14
We get Quiz added.
-
10:15
And if we look at Python Basics, now we have Review: Strings right here, and
-
10:19
we can click on Review: Strings.
You need to sign up for Treehouse in order to download course files.
Sign up