DEV Community

balt1794
balt1794

Posted on

Chapter 10: Delete Listing Page - Django 3...2...1...Takeoff! Series

Lastly, we need to create a page which will show the option to remove any particular listing.

Delete URL

Let’s open urls.py and add the path to the delete page.

Screen Shot 2020-09-21 at 9.16.37 PM

Delete View

For the delete view, we’ll have a very similar approach to the edit view. Open views.py and add the following code.

Screen Shot 2020-09-21 at 9.16.50 PM

listing = Listing.objects.get(id=delete_id)

We need to delete the listing that we select, therefore, if the listing we want to delete is the first listing, we’ll have the following, listing = Listings.objects.get(1). This will get all the objects of listing 1. In a similar way if we select a different listing.

if request.method == 'POST':
listing.delete()

Users will need to make a POST request in order delete a listing. We’ll give users the option to choose if they want to delete a listing or not. If they click the delete button, that will generate a POST request and delete that particular listing.

Delete Template

Create a template called delete_listing.html in the templates folder and add the following code.

Screen Shot 2020-09-21 at 9.16.59 PM

In this template, you can see that the method argument is a POST since we are making a request to delete a listing. We’ll display a message which will prompt users to choose, so if users click Delete, then the listing will be deleted. On the other hand, Cancel redirects users to my listings page.

My Listings Template Revisited

Before running the server, open my_listings.html and activate the delete button.

Screen Shot 2020-09-21 at 9.17.11 PM

Save the changes and run the server to see the delete page in action. You should see a message like the one below when trying to delete a listing.

ks

Notice that if you delete a listing, it disappears from both the listings page and my listings page. With the delete page, we have completed all the essential functions of the listings app. However, there’s always room for improvement. You can play around and add more functionality to the website if you like. Next chapter will move away from the listings app and create the users app, so that people can sign up and use the website to create listings.

Thanks to everyone who was following the series. Sadly, this is the last post of this series, however,if you would like to continue learning about Django and building the website, please consider buying the book which comes with the source code.

Django 3…2…1…Takeoff! Kindle

Django 3…2…1…Takeoff! Paperback

The leftover chapters of the book cover the following topics:

Chapter 11: Users App
Login Page

Chapter 12: Register Page

Chapter 13: Foreign Key

Chapter 14: Images

Chapter 15: Django Filters

Chapter 16: Styling with Bootstrap (Part I)

Chapter 17: Styling with Bootstrap (Part II)
Django Crispy Forms

Chapter 18: AWS - Amazon Relational Database Service (RDS)
Amazon RDS Creation
Connecting to Amazon RDS

Chapter 19: Amazon S3 Buckets
Amazon S3 Bucket Creation

Chapter 20: Heroku Deployment

Top comments (0)