问题描述
我目前正在尝试将两个Python字典排序到一个HTML数组中,例如:
I am currently trying to sort two Python dictionaries into an HTML array such as:
#Headers
DictA = {'value': 'valeur', 'name': 'nom' }
#Data
DictB = {'value': '456', 'name': 'Test' }
我想对这两个字典进行排序,以使DictB
中的'456'
等于DictA
中的键'value'
.
I wanted to sort these two dictionaries in order to get the '456'
in DictB
equals to the key 'value'
in DictA
.
注意:我使用了除DictA
和DictB
以外的其他词典,这只是一个示例.但这适合我的问题.
Note: I used other dictionaries than DictA
and DictB
, it was just an example. But it fits my problem.
在我的views.py
中,我定义了两个字典,例如:
In my views.py
, I define my two dictionaries such as:
headers = json.loads(entries[0].form_data_headers)
data = json.loads(entries[1].saved_data)
valuesData = data.values()
然后通过上下文将它们传递到template.html
中:
then I pass them into the template.html
via the context:
context = {'entries': entries, 'form_entry_id': form_entry_id, 'headers': headers, 'data': data, 'valuesData': valuesData}
因此,在我的模板中,它将打印一个包含标题(DictA
)和数据(DictB
)的数组.
Thus, in my template, it will print an array with the headers (DictA
) and the datas (DictB
).
在我的模板中,我编写以下代码:
In my template I make this code:
<thead>
<!-- Test pr voir si les values sont pris en compte ou non -->
<p>{{valuesData}}</p>
<tr>
{% for entry in headers %}
<td>
{{ entry }}
</td>
{% endfor %}
</tr>
</thead>
数据在另一个for
循环中:
<tbody>
<thead>
<!-- Test pr voir si les values sont pris en compte ou non -->
<p>{{valuesData}}</p>
<tr>
{% for entry in dataValues %}
<td>
{{ entry }}
</td>
{% endfor %}
</tr>
</thead>
</tbody>
结果有点类似:
- 名称等于456(而不是表单名称)
- geom等于测试(而不是我的坐标)
等
它与正确的标题不匹配.
It doesn't match the right header.
我正在考虑使用其中的if
语句进行两个for
循环:
I was thinking about making two for
loops with an if
statement in it:
{%if headers['name'] == dataValues['name']%}
<td>dataValues['name']</td>
但是我收到一个错误,因为无法解析dataValues['name']
.
But I get an error as dataValues['name']
could not be parsed.
其余代码使用Javascript:
The rest of the code is in Javascript:
{% endblock content %}
{% block javascript %}
<script type="text/javascript">
function GetURLParameter(param_name){
var sPageURL = window.location.search.substring(1);
var sParameterName = sPageURL.split('=');
return sParameterName[1];
}
var name= GetURLParameter(name);
document.querySelector('.page-header .value').innerHTML = name;
</script>
{% endblock %}
{% block main-content-inner-attrs %}
{% endblock main-content-inner-attrs %}
{% block sidebar-wrapper %}
{% endblock sidebar-wrapper %}
其余代码在javascript中:
The rest of the code is in javascript :
{% endblock content %}
{% block javascript %}
<script type="text/javascript">
function GetURLParameter(param_name){
var sPageURL = window.location.search.substring(1);
var sParameterName = sPageURL.split('=');
return sParameterName[1];
}
var name= GetURLParameter(name);
document.querySelector('.page-header .value').innerHTML = name;
</script>
{% endblock %}
{% block main-content-inner-attrs %}
{% endblock main-content-inner-attrs %}
{% block sidebar-wrapper %}
{% endblock sidebar-wrapper %}
推荐答案
这是正确的答案:
<tr>
{% for cle, valeur in headersLoop %}
{% for cleVD, valeurVD in valuesData %}
{% if cle == cleVD %}
<td>
<p> {{cle}}{{valeurVD}} </p>
</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
我将其标记为已解决.并创建一个新主题.谢谢大家.
I marked it as solved. And create a new topic. Thanks all.
这篇关于Django/Python:使用等号对python的字典进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!