1

I am hoping someone can show me where I am going wrong. In one template I have a table that displays a few rows of data. I want a link in one of the fields to open a separate template for that field.

The print commands display the correct information: print(vendor) displays: Sandpiper print(searchresult) displays: <QuerySet [<Vendor: Sandpiper>]>

Sandpiper matches the name of the vendor in the vendor table, but when executed the detail page loads but does not display any of the data from the Vendors table.

views.py
def utilvendorview(request, vendor):

    searchresult = Vendor.objects.filter(search=vendor)
    print(vendor)
    print(searchresult)
        
    return render(request,'utilityrentals/vendors_detail.html',{"searchresult":searchresult})

urls.py
path('utilvendorview/<vendor>/', views.utilvendorview, name='utilityvendor'),
index.html (main template)
<td><a href="utilvendorview/{{ results.Vendor  }}">{{ results.Vendor }}</a></td>

vendors_detail.html - Trying to have this populate
{% extends "maintenance_base.html" %}

{% load static from static %}

{% block body %}
<div class="album py-5 bg-light">
    <div class="container">
        <div class="row">
                <div class="col-md-4">
                    <div class="card mb-4 box-shadow">
                          <div class="card-body">
                           <h5 class="card-title">{{ Vendor.company }}</h5>
                           <h6 class="card-subtitle mb-2 text-muted">Email Address: {{ searchresult.email1 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Email Address: {{ searchresult.email2 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Phone: {{ searchresult.phone1 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Mobile: {{ searchresult.phone2 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Category: {{ searchresult.category }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Address1: {{ searchresult.address1 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Address2: {{ searchresult.address2 }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">City: {{ searchresult.city }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Province: {{ searchresult.province }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Postal Code: {{ searchresult.postal }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Notes: {{ searchresult.notes }} </h6>
                            <h6 class="card-subtitle mb-2 text-muted">Active: {{ searchresult.active }} </h6>
                            </div>
                    </div>
                </div>
        </div>
    </div>
</div>
{% endblock body %}

2 Answers2

2

You try my example

{% block body %}
<div class="album py-5 bg-light">
    <div class="container">
        <div class="row">
            <div class="col-md-4">
                <div class="card mb-4 box-shadow">
                    <div class="card-body">
                        <h5 class="card-title">{{ Vendor.company }}</h5>
                        {% for sc in searchresult %}
                        <h6 class="card-subtitle mb-2 text-muted">Email Address: {{ sc.email1 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Email Address: {{ sc.email2 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Phone: {{ sc.phone1 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Mobile: {{ sc.phone2 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Category: {{ sc.category }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Address1: {{ sc.address1 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Address2: {{ sc.address2 }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">City: {{ sc.city }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Province: {{ sc.province }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Postal Code: {{ sc.postal }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Notes: {{ sc.notes }} </h6>
                        <h6 class="card-subtitle mb-2 text-muted">Active: {{ sc.active }} </h6>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock body %}
Duy Black
  • 27
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 19 '23 at 13:26
  • This answer will be perfect if there will be multiple number of vendors in your searchresult. As the you will be returning an 'n' number of instances inside a queryset. Then in your template you will be looping through every instances which is basically done here. – Sanjay Shahi Apr 20 '23 at 04:38
1

The issue here seems that you are trying to get attributes of an instance from a queryset.

searchresult = Vendor.objects.filter(search=vendor)

As you have printed in terminal, this gives you a queryset. And you cannot get attributes from queryset as queryset is a list of instances not an instance.

To fix this,

searchresult = Vendor.objects.get(search=vendor)
Sanjay Shahi
  • 124
  • 1
  • 6