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