3

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.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
skateb2020
  • 129
  • 11
  • How about just `listing = Listings.objects.get(id=id)`? I mean, you're supposedly getting the id through the url parameter, but then trying to get the listing with id=2. Why? – raphael Apr 19 '22 at 02:29
  • @raphael if I do that I get a Page Not Found error. The error comes when I use the url with the number for the id. – skateb2020 Apr 19 '22 at 14:40
  • To troubleshoot that, I'd need to see your app's `urls.py`, and your `listing.html` template at least. – raphael Apr 19 '22 at 15:04
  • @raphael I added the urls.py and listing.html – skateb2020 Apr 20 '22 at 00:04

1 Answers1

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!

raphael
  • 2,469
  • 2
  • 7
  • 19
  • thank you so much. Would you mind telling me where I was going wrong? – skateb2020 Apr 20 '22 at 13:35
  • If you mean the page not found error, @skateb2020, then I would need to know how you were getting to that error. Were you manually putting in the link in the browser, and if so, what exactly were you putting in the address bar that led t the error. Since I did not see *any* links in your template, I must assume that is the only way this happened. As far as your original question, I believe you were trying to handle both the page that list **all** the listings with the page that should handle the details of a **singe** listing. But those must be handled with separate view/paths/templates. – raphael Apr 20 '22 at 15:18