【问题标题】:How to display querysets in Django?如何在 Django 中显示查询集?
【发布时间】:2021-11-21 11:21:04
【问题描述】:

我写了一个函数,最后,函数创建了几个模型对象(相同的模型不同的值)。我列出了这些对象,但我只想列出一个具有相同 dashboard_id 值的对象。 我发现了一些相关信息,但在表格中显示时遇到问题。

它正在工作,但不显示值。我该如何解决?

views.py

def reports_list(request):
    report_list = Reports.objects.values_list('dashboard_id', flat=True).distinct()
    context = {
        'report_list': report_list,
    }
    return render(request, "report_list.html", context)

report_list.html

<table id="table_id" class="display">
   <thead>
      <tr>
         <th>Kullanıcı</th>
         <th>Yüklenme Tarihi</th>
         <th>Görüntüle</th>
      </tr>
   </thead>
   <tbody>
      {%  for report in report_list %}
      <tr>
         <td>{{ report.user.first_name }} {{ report.user.last_name }}</td>
         <td>{{ report.created_at }}</td>
         <td>view</td>
      </tr>
      {% endfor %}
   </tbody>
</table>

要清楚:

【问题讨论】:

    标签: python html django


    【解决方案1】:

    我认为您在函数中的内容与我在模板中看到的意图不符。因此你可以试试这个:

       def reports_list(request, dashboard_id):
            report_list = Reports.objects.filter(dashboard_id=dashboard_id).all()
            context = {
                'report_list': report_list,
            }
            return render(request, "report_list.html", context=context)
    

    如果您认为您的原始功能是正确的,那么您忘记添加上下文(context=context)。

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2023-03-07
      • 2021-10-03
      • 2016-03-16
      • 2021-11-17
      • 2020-10-17
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多