I am unable to transfer the object to display_item.html page, from index page in Djanog page

I have been trying for a very long time to transfer item, which is obtained via a for loop in Django templating language. An item is an object. I have tried a lot of different arrangements, but nothing is working so far.

It's the index file.

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Active Listings</h2>
    {% for item in listings %}
    <a href="{% url 'display_item' entry=item  %}">
        <img src="/media/{{ item.image_url }}" width="300" height="400" alt="Picture of the item.">
        <h3>{{ item.title }}</h3>
    <p>{{ item.description }}</p>
    <h5>Starting bid: {{ item.starting_bid }}</h5>
    {% empty %}
    <h4>No listings to display!</h4>
   {% endfor %}
{% endblock %}

Its the file that is suppose to display the object.

{% extends 'auctions/layout.html' %}

{% block body %}
<img src="/media/{{ item.image_url }}" width="400" alt="Picture of item here.">
<p>{{ item.category }}</p>
<h3>{{ item.title }}</h3>
<p>{{ item.description }}</p>
<h5>Created by {{ item.created_by }}</h5>
<p>{{ item.created_at }}</p>
{% endblock %}

It's the URLS.py file.

from django.conf import settings 
from django.conf.urls.static import static

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("new_listing", views.new_listing, name="new_listing"),
    path("display_item/<str:entry>/", views.display_item , name="display_item")

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And finely it's my views.py file.

from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import Listing, Bid, Comment,User

def index(request):
    # return HttpResponse(f"The listings are: "+str(listings))
    return render(request, "auctions/index.html",{
                  "listings": Listing.objects.all() }

def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
        return render(request, "auctions/login.html")

def logout_view(request):
    return HttpResponseRedirect(reverse("index"))

def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."

        # Attempt to create new user
            user = User.objects.create_user(username, email, password)
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
        return render(request, "auctions/register.html")

def new_listing(request):
    if request.method == "POST":
        title = request.POST["title"]
        description = request.POST["description"]
        starting_bid = request.POST["starting_bid"]
        image_url = request.POST["image_url"]
        category = request.POST["category"]
        created_by = request.user

        # Attempt to create new listing
            listing = Listing.objects.create(
        except IntegrityError:
            return render(request, "auctions/new_listing.html", {   
                "message": "Error creating new listing."
        return HttpResponseRedirect(reverse("index"))
       return render (request, "auctions/new_listing.html")
def display_item(request, entry):
    if entry is None:
        ty = type(entry)
        return HttpResponse("It's null brao.")
        return HttpResponse("Type is "+ty)
    # return render(request, "auctions/item.html", {
    #     "item":entry
    # })

I tried chatGPT, I tried to check its datatype and render that but it's not visible. Checking my code a bunch of times.

I was expecting it to work.

So, in Django, we cannot directly pass objects via a URL. What we gotta do is just to pass via URL some way (usually ID) to the function that is going to render the subsequent page and then call that object on that view function directly.

Back to Top