2👍
I’m making a lot of assumptions here, so apologies, but I think the page not found error and your original question are related, and I think it all comes down to making two different views, two different url paths, and two different templates. One view/path/template for all listings (plural), and another view/path/template for the individual listing (singular).
First the urls you will need:
path("listings/", views.listings, name = "listings"),
path("listing/<int:id>/", views.listing, name = "listing"),
What you already have seems like it would belong to the listings view/path/template, with some modifications. The listings view will not have an id, since it will display all the listings (note that I changed all these to listings as opposed to listing so I don’t confuse this with the view that will take care of the single listing of a particular listing item:
def listings(request):
return render(request, "auctions/listings.html",{
"listings": Listings.objects.all()
})
This view can go with your listings template (note that I changed the name from listing.html to listings.html, since listing.html will take care of the particular listing, while listings.html will list all listings). Also note the <a href="{% url 'listing' listing.id %}">
part which is the the magical part where the url will be created with the particular listing’s id (note that the name of the view goes after the url word, and that the id does not have quotes – it will be the parameter added to the generated url):
{% extends "auctions/layout.html" %}
{% block body %}
{{% for listing in listings %}
<img src ="{{ listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ listing.title }}</h4>
<h6>Description: {{ listing.description }}</h6>
<h6>Category: {{ listing.category }}</h6>
<h6>Price: ${{ listing.bid }}</h6>
<a href="{% url 'listing' listing.id %}">link to this particular listing's page</a>
{% endfor %}
{% endblock %}
Now to the particular listing’s detail page, where the above generated link will take the user to.
def listing(request, id):
listing = Listings.objects.get(id=id)
return render(request, "auctions/listing.html",{
"listing": listing
})
Next you have to create a template called listing.html
where you can show all the details of the particular listing.
CS50 Web Programming course is fantastic, and very difficult. What helped me a lot was reading the notes, and also stopping the video and writing the code he demonstrates. All the answers are there. Just a suggestion; everyone learns differently. Good luck!