skip to Main Content

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

Answers


  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)
    
    Login or Signup to reply.
  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 %}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search