【问题标题】:Python Django Templates DictionariesPython Django 模板字典
【发布时间】:2018-10-22 09:00:40
【问题描述】:

我需要在我的模板中打印一个表格,其中包含已在我的视图中处理的数据。它在 collections.OrderedDict() 变量中:

table_dict[position] = {'date': item['date'],
                                    'document': item['document'],
                                    'details': item['details'],
                                    'quantity_balance': quantity_balance,
                                    'unit_price_balance': unit_price_balance,
                                    'total_price_balance': total_price_balance,
                                    'quantity_buy': 0,
                                    'unit_price_buy': 0,
                                    'total_price_buy': 0,
                                    'quantity_sell': 0,
                                    'unit_price_sell_avrg': 0,
                                    'total_price_sell_avrg': 0,
                                    'unit_price_sell': 0,
                                    'total_price_sell': 0,
                                    'earn_total_sell': 0}

我的职位不止一个,下面是Python的控制台打印示例:

{'total_price_sell_avrg': 0, 'quantity_balance': Decimal('1.000000000000000000'), 'date': datetime.date(2018, 5, 10), 'document': '9d4f8661', 'unit_price_sell_avrg': 0, 'total_price_sell': 0, 'total_price_balan
e': Decimal('2400000.000000000000000000000'), 'quantity_sell': 0, 'unit_price_buy': 0, 'details': 'Initial Investment', 'quantity_buy': 0, 'earn_total_sell': 0, 'unit_price_balance': Decimal('2400000.0000000000
0000000'), 'total_price_buy': 0, 'unit_price_sell': 0}

{'total_price_sell_avrg': 0, 'quantity_balance': Decimal('1.500000000000000000'), 'date': datetime.date(2018, 5, 10), 'document': '09asdashd', 'unit_price_sell_avrg': 0, 'total_price_sell': 0, 'total_price_bala
ce': Decimal('3750000.000000000000000000000'), 'quantity_sell': 0, 'unit_price_buy': Decimal('2700000.000000000000000000'), 'details': '08a7sd80a7doiadsiaud0a87ds', 'quantity_buy': Decimal('0.500000000000000000
), 'earn_total_sell': 0, 'unit_price_balance': Decimal('2500000.000'), 'total_price_buy': Decimal('1350000.000000000000000000000'), 'unit_price_sell': 0}

现在我有一个有序表,我想在我的 html 模板中打印它,它基本上具有这样的结构:

    <table class="tablerow">
        <tr>
            <th colspan="4"></th>
            <th colspan="3">Buys</th >
            <th colspan="3">Sells</th>
            <th colspan="3">Total</th>
        </tr>
        <tr>
            <th >Num T</th>
            <th >Date</th>
            <th >Document number</th>
            <th >Type Operation</th>
            <th >Details</th>

            <th >Quantity</th>
            <th >Unit Price</th>
            <th >Total Price</th>

            <th >Quantity</th>
            <th >Unit Price</th>
            <th >Total Price</th>

            <th >Quantity</th>
            <th >Unit Price</th>
            <th >Total Price</th>
        </tr>
        </table>

如何在我的 html 上打印我的字典来维护订单? 我将字典作为上下文传递给模板:

context = {'table':table}

非常感谢您的帮助!祝你今天过得愉快。 PD:我也尝试使用自定义过滤器标签,但我无法构建一个适用于这种情况的标签,也许我不明白它们如何适用于这种情况。

【问题讨论】:

    标签: python django dictionary django-templates


    【解决方案1】:

    要在 html 中打印字典,您可以这样做:

    {% for key, value in table.items %}
        <p>I am dictionary key: {{ key }} </p>
        <p>I am value of dictionary key: {{ value}} </p>
    {% endfor %}
    

    只有一刻 - 你的 table 变量真的是 dict 吗?看起来它是一个字典列表,因为你说 ...I has more than one positions... 在这种情况下它可能看起来:

    {% for t in table %}
        <p>{{ t.date }}</p>
        <p>{{ t.document }}</p>
        <p>{{ t.details }}</p>
        .....
    {% endfor %}
    

    【讨论】:

    • 感谢您的帮助,我用它来获得更好的主意,但我必须编写一个自定义标签过滤器,所以我将 dict 转换为按值排序的列表列表进入,这解决了我的问题。谢谢你的回答。 !!
    【解决方案2】:

    您可以在 django 模板中使用点 . 访问 dict {{ dict.key }} 返回 {{ value }}

    变量名中的点表示查找

    在你的情况下:

    {{ table.date }} <!--returns the date-->
    {{ table.document }} <!--the document-->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 2012-07-27
      • 2014-01-23
      • 2021-04-10
      相关资源
      最近更新 更多