简体   繁体   中英

how to find a dictionary value in django template

I have a dictionary.

{21: [<Trade: SPY   190204C00271000>], 20: [<Trade: SPY   190201P00265000>]}

I wrote a filter to get the value from dict in Django template.

from django import template
register = template.Library()

@register.filter
def tradeItem(inputs, i):
    return inputs.get(i)

in the template:

{{ openTrades|tradeItem:trade.trade_number }}

Now I am able to get this specific trade. but I need to get value of trade.price. how do i do that? I tried.

{{ openTrades.price|tradeItem:trade.trade_number }}
{{ (openTrades|tradeItem:trade.trade_number).price }}

Just extend your filter to get the price attribute instead of the trade object, eg

from django import template
register = template.Library()

@register.filter
def tradeItemPrice(trades, i):
    return trades.get(i).price

then in the template

{{ openTrades|tradeItemPrice:trade.trade_number }}

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