Home > Back-end >  How do I get the id of the model item that is clicked on?
How do I get the id of the model item that is clicked on?

Time:07-25

My main page on my website shows all the possible listings and when one of them is clicked on, the user is meant to be taken to a page that shows the listing's information. (This page is not finished yet.) This means that I need to be able to know which listing has been clicked on so I need a way to get the id of the listing.

I've tried to do this by taking in a id parameter/argument that should be used inside the function to find out which listing this is.

I want the url to be http://127.0.0.1:8000/[id]/[name of listing] where [id] and [name of lsiting] and the actual ids and names of the listings. id should be a number and name_of_listing should be a string. (The id is in Listing.id and the name of the listing is in Listing.name - Listing is the name of the model and the id and name are fields.)

How do I get the id of a listing through a link?

index.html: (this just displays all the listings - I've used comments to show where the links are)

<main>
    <div >
        {% for i in listings %}
        <div >
            {% if i.photo_present != None %}
            <div >
                {% if i.photo_url != None %}
                <div >
                    <a href="{% url 'listing' i.id %}"> <!-- this is where the link is -->
                        <img src=" {{ i.photo_url }} " alt="">
                    </a>
                </div>
                {% else %}
                <div >
                    <a href="{% url 'listing' i.id %}"> <!-- this is where the link is -->
                        <img src=" media/{{ i.photo_upload }} " alt="">
                    </a>
                </div>
                {% endif %}

                {% else %}
                <div >
                    <img src="media/images/no-image.jpg" alt="No Image">
                </div>
            </div>
            {% endif %}
            <div ></div>
            <div >
                <div >
                    <div >
                        <a href="{% url 'listing' %}"> <!-- this is where the link is -->
                            <h4>{{ i.name }}</h4>
                        </a>
                    </div>
                    <div >
                        <a href="{% url 'listing' %}"> <!-- this is where the link is -->
                            <strong>£{{ i.highest_bid }}</strong>
                        </a>
                    </div>
                </div>
                <div >
                    <a href="{% url 'listing' %}"> <!-- this is where the link is -->
                        <p>{{ i.description }}</p>
                    </a>
                </div>
            </div>
        </div>
        <hr>
        {% endfor %}
    </div>
</main>

views.py: (attempt to get the listing id)

from .models import Listing # this is the name of the model

def listing(request, id):
    listing = Listing.objects.get(pk=id)
    return render(request, 'auctions/listing.html', {
        'listings': Listing.objects.all(),
        'listing': listing
    })

urls.py:

path('<int:id>/<str:name>', views.listing, name='listing')

listing.html: (what should be shown when the user clicks on the link)

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

{% block body %}
<h1>
    {{ listing.id }}
</h1>
{% endblock %}

CodePudding user response:

To get the id use the 'pk' attribute, remember that in the url you also requested the name of the list

{% url 'listing' i.pk i.name %}

Also correct the url by inserting a / at the end

path('<int:id>/<str:name>/', views.listing, name='listing')

In other parts of the code you used the following link

{% url 'listing' %}

Would this link be the same as the previous one or a separate page? if it is another page you must implement a new url without parameters

  • Related