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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Lastly, we need to be able to delete a quote and again, this is pretty similar. So I'm going to copy this code over
0:13 and this copy-pasting of code is a bit uncomfortable. But we will see later, with the class-based view that we can abstract this away
0:21 and it will look a lot cleaner. In this case, we want to retrieve the quote with get_object_or_404 again. We don't need the for because
0:32 we're just going to delete the quote we're not going to edit it and here I do an if request method equals post and we're going to quote_delete
0:49 and remember from our Django ORM lesson that if you load a quote object in a variable and you call delete upon it then it's gone
0:58 it's committed to the database. You don't need an extra save that's all done within the delete method.
1:08 The success message then changes to deleted quote we're going to redirect to the quote list that's the same
1:18 the only thing is I want to have a new template to actually confirm that the user really wants to delete the quote
1:28 so we prevent that a user by clicking delete immediately deletes the quote I want to go to a separate template to really make sure that
1:37 that's what they want I'm going to paste back the quote and no form here so actually this works the following if the quote_delete view is called
1:49 the first time we're going to render this quote_delete HTML which is basically a confirmation template containing another form
1:58 if that form gets submitted you get a post request and then we know the user confirmed the action and we need to delete the quote
2:09 once that's done we reroute to the quote list so next up we need to generate this template intofor the HTML I'm going to copy that in
2:26 again we extend from the base.html we occupy our block content we have a header of confirm delete quote we have our second form
2:36 which again needs our security token and we print the quote and a question are you sure if you want to delete it? We make a danger button
2:47 which will be read that says delete and a go back button when this gets submitted that means we hit this post request in that case we delete the quote
3:03 let's try this out great I go to the new page you can go back nothing happens I can try it again delete. Bang. Gone.
3:22 there's no deletion here you have to do it here yes gone great so that is a fully create read update and delete app and we've come very far
3:37 I mean this is a fully working app and the only thing left now is to go back to these function based views which are great
3:47 but there's some repetition here going on we do this get_object_or_404 every time the edit code was pretty similar to the new code
3:56 and in the next video I'm going to show you how we can rewrite this using class based views and we will see it dramatically reduces the code
4:04 needed to accomplish the same thing


Talk Python's Mastodon Michael Kennedy's Mastodon