【问题标题】:How to fix context dict in CBV view in django?如何在 django 的 CBV 视图中修复上下文字典?
【发布时间】:2023-04-04 17:04:02
【问题描述】:

我有这个查看和获取上下文数据的方法
View 在视图代码的末尾,我将差异字典放入上下文键中。然后我尝试将所有数据输出到模板中,但最后 3 个字段为空。 这是模板代码:Template for view。 在视图结束时,我打印差异。
Console prints 它为我提供了我需要的所有数据。

[{'depos': Decimal('400000.00'), 'df': Decimal('-4600000.00'), 'pr': Decimal('-38722782.00'), 'creds': Decimal('5000000.00'), 'date': (2016, 3), 'dc': 'Створити акційні пропозиції, переглядати або створити депозитні програми'}, 
{'depos': Decimal('450776.00'), 'df': Decimal('-33783479.00'), 'pr': Decimal('-38672006.00'), 'creds': Decimal('34234255.00'), 'date': (2016, 2), 'dc': 'Створити акційні пропозиції, переглядати або створити депозитні програми'}, 
{'depos': Decimal('3473400.00'), 'df': Decimal('2973399.98'), 'pr': Decimal('-35649382.00'), 'creds': Decimal('500000.02'), 'date': (2016, 4), 'dc': 'Слід знижувати відсоткові ставки в депозитних програмах'}]

我想输出该字典中的所有字段。如何在模板/视图中解决此问题?

【问题讨论】:

    标签: python django dictionary django-templates django-views


    【解决方案1】:

    在最后四个语句中,您使用的不是dif,而是difs

    {% for dif in difs %}
        <tr>
            <td>{{ dif.depos }}</td>
            <td>{{ dif.creds }}</td>
            <td>{{ dif.date }}{{ difs.df }}</td>   <--- should be dif.df
            <td>{{ difs.df }}</td>                 <--- should be dif.df 
            <td>{{ difs.dc }}</td>                 <--- should be dif.dc 
            <td>{{ difs.pr }}</td>                 <--- should be dif.pr
        </tr>
    {% endfor %}
    

    【讨论】:

    • 真是的,我没发现孩子们的错误(
    • 等一下,再等 15 秒,我做到了)
    猜你喜欢
    • 2020-02-17
    • 2018-04-04
    • 2020-08-13
    • 2017-04-10
    • 2017-05-25
    • 2023-04-11
    • 2017-08-31
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多