简体   繁体   English

在Heroku上运行时Django数据丢失顺序

[英]Django data losing order when running on Heroku

I have a list which contains lists like [[1, 2, 4], [5, 6,7], [8, 9,10]] I pass the list to the template html which then loops through it and displays it as a table like below: 我有一个包含[[1, 2, 4], [5, 6,7], [8, 9,10]]之类的列表的列表,我将该列表传递给模板html,然后它循环遍历并显示为如下表:

<tbody>
 {% for zone in zones %}
 <tr>
  <td>{{zone.2}}</td>
  <td>{{zone.0}} -- {{zone.1}}</td>
   </tr>
  {% endfor %}
 </tbody>

This is how I'm passing it: 这是我通过的方式:

return render(request, 'alpha/index.html', {"zones":zones})

When I'm running it locally it works as I expect it. 当我在本地运行它时,它按预期运行。 That is, the first element in the list is presented first in the table as well. 也就是说,列表中的第一个元素也首先出现在表中。 This order is lost when I push to Heroku (the zones appear randomly in the table) and I'm not sure what is causing this. 当我按下Heroku时,该顺序丢失了(区域随机出现在表格中),我不确定是什么原因造成的。

I have tried to take the elements of the list, load them into variables and pass them one by one to see if the list is losing the order somehow. 我试过采用列表中的元素,将它们加载到变量中,并一一传递,以查看列表是否因某种原因失去了顺序。 This didn't help, so I'm thinking it's a problem either with Heroku's setup or I'm using Django wrong. 这没有帮助,所以我认为这是Heroku的设置存在问题,或者我使用Django错误。

Any help is much appreciated. 任何帮助深表感谢。 Thanks! 谢谢!

I solved this problem by passing the sorted list like so: 我通过传递排序列表来解决此问题,如下所示:

return render(request, "index.html", {"zones":sorted(zones.values())})

I'm still not sure why it's messing up the order (maybe Heroku or Django) but this did it for me. 我仍然不确定为什么要弄乱订单(也许是Heroku或Django),但这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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