【发布时间】:2022-09-27 15:42:39
【问题描述】:
我正在开发一个显示帖子的网络(如 twitter)。在 Django views.py 中,我编写了一个创建两个数组的代码,并分配了要在 HTML 模板中使用的数组。 视图.py:
def arrays(request):
allposts = posts.objects.all()
m = [\'empty\', \'like\', \'unlike\', \'like\', \'unlike\']
aa = [0, 1, 2, 3, 4]
return render(request, \"network/index.html\" ,{\'allposts\': allposts, \'m\':m, \'aa\':aa})
(m) 数组表示每个帖子是否被点赞(数组中的每个对象的排列方式与帖子 id 相同),而 (aa) 表示数据库中每个帖子的 id
在 index.html 中,我想根据数组中的排列为每个帖子显示 \'like\' 或 \'unlike\'。
在 index.html 中
{% for post in allposts %}
<div>
{% for object in aa %}
{% if object == post.id %}
<p>{{m.object}}</p>
{% endif %}
{% endfor %}
</div>
{%endfor %}
但问题是我无法匹配 HTML 模板中的 aa 数组和 m 数组,但我可以显示 {{m.1}} 而不是 {{m.object}}。那么我怎样才能匹配这两个数组呢?
标签: python html arrays django web