首页 > 解决方案 > 为什么我的 For 循环在 django 模板中不起作用

问题描述

我正在尝试获取一笔交易的所有发票。因为有部分支付的可能性,这就是为什么我需要获取交易历史。我已经在我当前的 HTML 中包含了另一个 HTML,其中包含 'include' 和 with。但在 for 循环结束时,它没有显示任何内容,同时也没有错误。

这是我的views.py:

def transaction_history(request, transaction_id):
    transaction = get_object_or_404(Transaction, pk=transaction_id)
    invoices = Invoice.objects.filter(transaction=transaction).order_by('-created_at')
    return render(request, 'front/transaction_history.html', {'invoices': invoices, 'transaction': transaction })

这是我的模板:

{% for invoice in invoices%}
    <tr>
       <td>{{ invoice.created_at }}</td>
       <td>{{ invoice.created_by }} </td>
       <td>{{ invoice.amount|floatformat:-2 }}</td>
       {% for item in transaction %}
          <td>{{ item.is_payed }}</td>
       {% endfor %}}
    </tr>
{% endfor %}

这是我的包含链接:

{% include 'front/transaction_history.html' with invoices=invoices only %}

标签: pythondjango-templates

解决方案


推荐阅读