#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to edit a quote
0:01 Next we're going to add an existing quote.
0:04 And the code is actually pretty similar
0:05 so we can just copy this over.
0:14 And there are three small differences.
0:16 First I need to get the quote.
0:21 I'm going to copy over the
0:23 get_object_or_404 from the quote_detail.
0:28 And it receives the primary key as passed into the function.
0:33 This should retrieve our quote.
0:36 We want to add it.
0:37 And the second small change to this function
0:40 as compared to the quote_new, is to pass it as an instance.
0:47 So I'm going to pass it to the form as an instance
0:51 and the QuoteForm, or Django's ModelForm
0:55 then knows that it needs to edit that quote
0:58 as opposed to adding a new one.
1:01 This logic is the same, the only thing
1:04 I'm going to say, updated quote.
1:08 The redirect is the same, I'm going to list
1:10 after a successful update
1:14 and a small change to the render
1:17 I'm going to use the same quote form template.
1:22 And additionally, I'm going to pass in the quote object.
1:31 Line it up as per PEP 8, and that should be it.
1:38 Let's try it out.
1:43 Wow, look at that, I went to edit four.
1:49 This code called a get_object_or_404
1:54 on primary key equals four, retrieved the quote
1:59 and now I can just edit it.
2:06 And nice, that persisted, and the message changed.
2:12 Let's add a source, look at that
2:19 This is definitely CSS, I mean the styling is
2:23 but this is all stuff that comes with Django
2:26 if you use the forms.
2:28 So let's give a URL
2:35 doesn't really matter for now.
2:38 And it got accepted.
2:43 And here from the view, I can also click Edit
2:47 and it persisted.
2:48 Great, look at that.
2:51 Almost the same code, just a few tweaks.
2:53 And we got our Edit View function done.