Python 如何在Django queryset中在模板中分别显示关键字和-值?
我在浏览器中从HTML模板获得此输出:Python 如何在Django queryset中在模板中分别显示关键字和-值?,python,django,django-templates,django-queryset,django-template-filters,Python,Django,Django Templates,Django Queryset,Django Template Filters,我在浏览器中从HTML模板获得此输出: {'coin__name': 'Bitcoin', 'total': Decimal('1498824')} {'coin__name': 'Ripple', 'total': Decimal('335227')} 如何在html模板中分别显示键和值(不说十进制) 期望的结果: Bitcoin, 1498824 Ripple , 335227 我在下面提供了查询和html模板: 视图。py: test = filtered_transaction_q
{'coin__name': 'Bitcoin', 'total': Decimal('1498824')}
{'coin__name': 'Ripple', 'total': Decimal('335227')}
如何在html模板中分别显示键和值(不说十进制)
期望的结果:
Bitcoin, 1498824
Ripple , 335227
我在下面提供了查询和html模板:
视图。py:
test = filtered_transaction_query_by_user.values('coin__name').annotate( total = (Sum('trade_price' ) * Sum('number_of_coins'))).order_by('-total')
template.html
<table class="table table-striped">
<tr>
<th>Current dict pair</th>
<th>Just the name of the crypto</th>
<th>Just the price of the crypto</th>
</tr>
{% for item in test %}
<tr>
<td>{{ item }}</td>
<td>{{ }}</td>
<td>{{ }}</td>
</tr>
{% endfor %}
当前dict对
只是密码的名字
只是密码的价格
{测试%中的项的%1}
{{item}}
{{ }}
{{ }}
{%endfor%}
尝试从循环中的字典中获取键和值:
{% for key, value in test.items %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
{%用于键,test.items%中的值]
{{key}}
{{value}}
{%endfor%}
如果要格式化十进制值,请使用以下代码更新模板:
{{ test }} <!-- test is list of dictonaries -->
<br>
{% for item in test %} <!-- Loop to get each item(sub dictonary) from list-->
<br>
{% for key,value in item.items %} <!-- Getting key values pairs from each sub dictonary item -->
{% if forloop.last %} <!-- Checking if last iteration of loop just to add "::" after each value -->
{{ value }} <!-- only displying values not keys from each sub dictionary -->
{%else%}
{{value }} ,
{% endif %}
{% endfor %}
{% endfor %}
{{test}
{测试%中的项的%1}
{%为键,值在item.items%}
{%if-forloop.last%}
{{value}}
{%else%}
{{value}},
{%endif%}
{%endfor%}
{%endfor%}
有关从结果中删除小数点的信息,请参阅此答案。
项目0和项目1给了你什么?谢谢@Ashish Nautiyal.)然而,我试图让它出现在一张整洁的桌子上。我不能,因为所有硬币价值对都显示在一列中。你能建议我如何修改这张表吗?如果我将
{{value}}
插入HTML代码,则所有硬币值对将显示在一列中。