Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Edit an Instance9:48 with Kenneth Love
Creating new instances from model forms is easy enough, but what about editing and saving an existing instance?
As you can probably guess the creation of our editing side 0:00 is pretty similar to the creation of our creating side. 0:05 So let's go over here to views, 0:11 let's close these two here we don't need those anymore. 0:12 Let's go over here to views and 0:15 we're gonna make a view very similar to quiz create for quiz edit. 0:17 And in fact, you could combine these into one view that had an optional 0:21 quiz that it went and found. 0:27 If it didn't find a quiz or wasn't given a quiz then it would create a new one. 0:28 But I'm gonna keep them as separate for 0:32 now and I'll leave combining them as an exercise for you. 0:34 So, at log in required, def quiz edit, 0:37 request course pk, quiz pk, and 0:41 right here you'd want to do none if you want to make that optional. 0:45 All right, that's my last, that's my last tent until we get to URLs probably. 0:54 Okay. So, quiz, 0:59 we need to do a get_object_or_404, 1:01 and models.Quiz, pk = quiz_pk, 1:06 course_id = course_pk. 1:11 That way we don't have to do, we don't have to work our way. 1:15 We can just stay right inside the quiz model. 1:20 And then, so we know we've got a course, or 1:23 we know if we've got a quiz, and so now let's make our forms. 1:26 So our form is going to be forms.QuizForm, and 1:30 this is a new thing as well, we're editing a specific quiz. 1:35 So we already have the quiz as a model, so we want to pass it to the form. 1:38 And we can't just put quiz in there. 1:45 We need to tell it that this is the instance. 1:48 This is where it's starting from. 1:51 So there we go. 1:55 We have that. 1:56 You know what, let's go ahead and build the rendering bit and we'll come back and 1:57 handle the saving bit. 2:02 So we wanted to return render request, 2:04 courses_quiz form dot html 2:07 and we wanna pass in form, form, and 2:14 course as quiz.course, cuz that's what it is. 2:18 That's how we work our way back. 2:24 Okay, cool. And then we need to make a URL for this. 2:26 And so I'm actually going to copy, actually wanna copy this one. 2:29 And we're gonna put it right here, take out that q, cuz that's not important. 2:37 And this'll be quiz_pk. 2:44 But here we're gonna put edit_quiz. 2:47 And this will be quiz_edit, and 2:53 our name will be, we'll say, edit_quiz. 2:57 So we have the course_pk, we have the words edit_quiz, we have the quiz_pk. 3:03 We go to the quiz_edit view and we name it edit_quiz. 3:07 All right, pretty simple. 3:13 So let's go here to quiz detail, and 3:15 we want to have the ability to edit this quiz. 3:18 So let's come down here and 3:22 again do if user.is_authenticated, 3:26 authenticated, put in the hr and 3:31 we'll put in a URL courses:edit_quiz, and 3:36 we're gonna say that course_pk=step.course.pk and 3:43 quiz_pk=step.pk because the step is the quiz. 3:51 Close that quote. 4:00 Class="button" and we'll say 4:01 edit and then we'll put in our endif. 4:05 All right, so we've got our url, we've got our view, we've got our quiz_detail. 4:12 So let's come over here, refresh this, and 4:18 now we've have an edit button, so that's cool. 4:22 Let's hit edit. 4:24 And you can see that we already have our stuff filled in. 4:25 But it says make a new quiz and 4:28 it says new quiz up here in the url, so let's fix those. 4:30 Let's come over here to our quiz form and 4:34 what we're gonna do right here is we want to instead of always just saying 4:39 new quiz, we wanna put in the name of the quiz if it exists. 4:46 So, let's print here. 4:50 What all to be sent to the view? 4:52 Let's look here at the view. 4:55 We sent the form, all right. 4:57 So, our form. 4:58 Remember we passed instance into the form? 5:04 We can get back to that instance. 5:06 And then we can say title. 5:09 And then we can do a default here. 5:11 And if there is no form instance or 5:13 there is no instance title, then the default comes through. 5:15 And we'll just say new quiz. 5:19 All right. 5:23 So easy enough on that bit. 5:24 And we wanna do the same thing here. 5:26 So it has make a new quiz right now. 5:28 But let's do form.instance.title or the default will be make a new quiz. 5:31 All right so there's that, refresh, cool, 5:40 now it says review strings because that's the name of our quiz that we're editing. 5:44 All right, so now we need to handle saving it, cuz if I hit Save right now, 5:50 it's actually going to make a new instance of the thing. 5:55 And we don't want a new, actually it won't, it'll just come right back. 6:03 Sorry. I don't know why I said 6:08 it'll make a new one. 6:09 If I hit Save, we just come right back to here, nothing happens. 6:09 So let's make something happen, shall we? 6:14 It sounds like a good idea to me. 6:17 All right, so we have our quiz, we have our form, 6:19 and you can probably guess my next step here which is going to be if 6:22 request.method ='POST', and then we want to do something. 6:27 Now what do we want to do? 6:34 First of all, we want to make a new form. 6:36 And we still want to say that the instance=quiz that we found. 6:40 But then we want to say that the data, the new stuff is request.POST. 6:46 We can do this. 6:51 We should be able to do this the other way around where we do request.post. 6:52 And then the instance. 6:57 But I kinda like it the other way a little better. 7:01 I think it's just a bit easier to read and to know exactly what's going on. 7:03 All right, so if that's all good, and 7:07 then we do if form.is_valid. 7:11 So the form is valid, and we're super happy. 7:15 Then we're just gonna do form.save. 7:18 We just wanna update the ones already there. 7:20 Because the course hasn't changed, the ID on the quiz hasn't changed. 7:22 Nothing like that's changed. 7:27 We just wanna save back over. 7:28 The one that's there, with the new data. 7:30 And then again, we wanna send out a message. 7:33 And we've been doing this messages.addmessage, 7:36 there's a shortcut for that. 7:39 We can do messages.success and that automatically sets it as a success. 7:40 And we can just hit request and then we'll just say updated and 7:46 we'll put in the title. 7:50 So format quiz. 7:53 Actually, let's do form.clean_data title because it might have changed, 7:56 somebody might have renamed it. 8:02 And then we want to return HttpResponseRedirect 8:04 (quiz.get_absolute_url. 8:08 All right, so this view is a little bit shorter than our creation view. 8:16 You can kind of see where those two things are different. 8:22 But let's test this. 8:24 So we called it review strings. 8:26 Let's call it review strings and things and 8:27 we'll say Save Message is not defined. 8:31 It's not, it's messages. 8:35 So, let's refresh that again and we'll say Continue, cool. 8:39 Now, it says Review: Strings and Things and up here, 8:44 it says Updated Review: Strings and Things. 8:46 So, that's pretty cool. 8:49 That's nice that we're able to do this double layer. 8:50 And hopefully like I said, you can see how to combine these into a single view. 8:54 And, just like that, we have a quick and easy way to edit a quiz. 8:59 I know this is a bit slap dash, just having a few special views for 9:03 editing just a few special models. 9:07 And yeah, I know, special cases aren't special enough. 9:09 In a more real world scenario though, you probably build an entire CMS 9:12 similar to the view and button we've created in this video. 9:15 We aren't going to do that in this course simply because it's out of the scope 9:19 of a course. 9:21 I heavily encourage you to create those other forms and views yourself though. 9:22 You can start with adding an add text step view. 9:26 Why not just do all of this in the admin? 9:30 Well, heavily modifying the admin is a special skill in and of itself. 9:32 And we'll be addressing that in another course. 9:36 Aside from that, some things are really hard to set up in the admin and 9:39 usually a custom CMS is just a better decision all around. 9:42 I think you'll see more of what I mean in the next video. 9:45
You need to sign up for Treehouse in order to download course files.Sign up