Django模板不显示表数据

jtw3ybtb  于 2023-04-22  发布在  Go
关注(0)|答案(2)|浏览(197)

我希望有人能告诉我,我在哪里出错。在一个模板,我有一个表,显示了几行数据。我希望在其中一个领域的链接打开该领域的一个单独的模板。
打印命令显示正确的信息:print(vendor)显示:Sandpiper print(searchresult)显示:〈QuerySet [〈Vendor:矶鹬〉]〉
Sandpiper匹配vendor表中的供应商名称,但执行时,详细信息页加载但不显示Vendors表中的任何数据。

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 %}
hm2xizp9

hm2xizp91#

你试试我的例子

{% 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 %}
lf5gs5x2

lf5gs5x22#

这里的问题似乎是您试图从查询集中获取示例的属性。

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

正如你在terminal中打印的那样,这给了你一个queryset。你不能从queryset中获取属性,因为queryset是一个示例列表而不是一个示例。
为了解决这个问题

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

相关问题