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.
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.