balt1794
Posted on September 20, 2020
In this chapter, we’ll create a page where we can keep track of our listings, add new listings, edit, and delete them too.
My Listings URL
Open urls.py and add the following code.
We have been repeating multiple times the process of creating pages. The process will not change much, so if there’s nothing new to explain, I will just add the code and go to the next section.
My Listings View
my_listings = Listings.objects.order_by('-list_date')
The entire my_listings view is just a copy of the all_listings view. This is the case because we haven’t created users yet, so at this point all listings belong to one person. Once we have different users creating listings, then we’ll come back to fix this view.
My Listings Template
Create a template called my_listings.html in the templates folder and add the following code.
a class="btn" href="% url 'listings:detail' my_listing.id %">View/a
Similar to the listings page, if users want to see a particular listing in detail, we can point them to the detail listing page that we have already created. Don’t forget to pass the ID, in this case, we use my_listing.id because my_listing is the variable being used in this particular template.
We also added two more buttons that we’ll activate in the following chapters which let users edit and delete their listings. Finally, we added the link to the new_listing page, so that users can create listings from this page rather than having the option in the navbar.
Navbar Revisited
Make some changes to the navbar, so that we can have the link to my_listings page.
Open base.html and change the code as follows.
After you make the changes, the navbar should have my listings instead of new listing. Once you go to my listings page, you’ll see a page like the one below.
Test the link to view the details of each listing. The other two links edit and delete are disabled for now. Disregard the style for now, we’ll make it look pretty soon.
If you're enjoying the series and want to support, you can find the entire book below.
Posted on September 20, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.