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