#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to edit a quote

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Next we're going to add an existing quote. And the code is actually pretty similar so we can just copy this over.
0:15 And there are three small differences. First I need to get the quote. I'm going to copy over the get_object_or_404 from the quote_detail.
0:29 And it receives the primary key as passed into the function. This should retrieve our quote. We want to add it.
0:38 And the second small change to this function as compared to the quote_new, is to pass it as an instance.
0:48 So I'm going to pass it to the form as an instance and the QuoteForm, or Django's ModelForm then knows that it needs to edit that quote
0:59 as opposed to adding a new one. This logic is the same, the only thing I'm going to say, updated quote. The redirect is the same, I'm going to list
1:11 after a successful update and a small change to the render I'm going to use the same quote form template.
1:23 And additionally, I'm going to pass in the quote object. Line it up as per PEP 8, and that should be it. Let's try it out.
1:44 Wow, look at that, I went to edit four. This code called a get_object_or_404 on primary key equals four, retrieved the quote
2:00 and now I can just edit it. And nice, that persisted, and the message changed. Let's add a source, look at that
2:16 some JavaScript saying, please enter URL. This is definitely CSS, I mean the styling is but this is all stuff that comes with Django
2:27 if you use the forms. So let's give a URL doesn't really matter for now. And it got accepted.
2:44 And here from the view, I can also click Edit and it persisted. Great, look at that. Almost the same code, just a few tweaks.
2:54 And we got our Edit View function done.


Talk Python's Mastodon Michael Kennedy's Mastodon