简体   繁体   中英

Python Django Templates Dictionaries

i need to Print a table in my template of a data that a already Processed in my View. It is in a collections.OrderedDict() variable as:

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}

I has more than one positions, here is an example of the console print of 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}

Now i have and Ordered table where i want to print it put in my html template, that has basically this structure:

    <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>

How can i print my dictionarie on my html maintaning the order? I pass the dictionary as a context to the template as:

context = {'table':table}

I really apreciate your help! Have a nice day. PD: i also tried to use custom filter Tag but i cant built one that works for this case, maybe im not understanding how they work for this case.

You can access dict in django templates with dot . {{ dict.key }} returns {{ value }}

A dot in a variable name signifies a lookup

In your case:

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

To print a dictionary in html you can do this:

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

Only moment - is your table variable really a dict ? Looks like it is a list of dicts, cuz u said ...I has more than one positions... In that case it might look:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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