繁体   English   中英

Python Django模板字典

[英]Python Django Templates Dictionaries

我需要在我的视图中已处理的数据模板中打印一张表。 它在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}

现在我有一个Ordered表,我要在其中打印它的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:我也尝试使用自定义过滤器标记,但我无法构建适用于这种情况的过滤器标记,也许我不了解它们在这种情况下的工作方式。

您可以使用dot访问Django模板中的dict . {{dict.key}}返回{{value}}

变量名称中的点表示查找

在您的情况下:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM