Python Django模板词典

2024-09-26 18:12:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在我的模板中打印一个表,其中包含一个已经在我的视图中处理过的数据。它位于collections.orderedict()变量中,如下所示:

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}

我真的很感激你的帮助!祝您有个美好的一天。 我也试过使用自定义过滤器标签,但我不能建立一个适用于这种情况下,也许我不明白他们如何为这种情况下的工作


Tags: datetableunitbuydetailsdocumentpricequantity
2条回答

要用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 %}

使用dot.{{dict.key}}返回{value}}

变量名中的点表示查找

就你而言:

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

相关问题 更多 >

    热门问题