I am doing CS50 Project 2 and need to make a webpage that takes in the id through the url and retrieves all of the information about that particular item from the database to display on the webpage.
def listing(request, id):
id = Listings.objects.get(id=2)
return render(request, "auctions/listing.html",{
"listings": Listings.objects.all()
})
This is my current code. I am trying to get the id that is in the url parameter through the variable, and I want the listings variable to only get the objects for that item, but I am not exactly sure how to do that.
Let me know if you need any more of the code. Thank you so much for all your help!
urls.py
from django.urls import path
from .models import User, Listings
from .forms import ListingsForm
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("create", views.create, name="create"),
path("watchlist", views.watchlist, name="watchlist"),
path("categories", views.categories, name = "categories"),
path("listings/<int:id>/", views.listing, name = "listing"),
path("register", views.register, name="register")
]
listing.html
{% 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>
{% endfor %}
{% endblock %}
I know that I will have to change the listing.html once I have figured out how to get the id to display only the objects for that id, but for the time being this is my listing.html
CodePudding user response:
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!