Django/Python:使用等号对python的字典进行排序

编程入门 行业动态 更新时间:2024-10-26 04:25:48
本文介绍了Django/Python:使用等号对python的字典进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在尝试将两个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的字典进行排序

本文发布于:2023-11-28 22:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:等号   字典   Django   Python   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!