简体   繁体   English

在 Django 中结合两个 arrays

[英]combining two arrays in Django

I'm working on a web that displays posts (like twitter).我正在研究显示帖子(如 twitter)的 web。 In Django views.py I wrote a code that makes two arrays and assigned the arrays to be used in the HTML template.在 Django views.py 中,我编写了一个代码,该代码生成了两个 arrays 并分配了 arrays 以在 Z4CZDBAD4FCA2E7AAFF4 模板中使用。 views.py:视图.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})

the (m) array represents whether each post is liked or not(each object in the array has the arrangement which is equal to the post id) while the (aa) represents the id of each post in the database (m) 数组表示每个帖子是否被点赞(数组中每个 object 的排列与帖子 id 相同)而 (aa) 表示数据库中每个帖子的 id

in index.html I want to show 'like' or 'unlike' for each post according to the arrangement in the array.在 index.html 我想根据数组中的排列为每个帖子显示“喜欢”或“不喜欢”。

in index.html在索引中。html

{% for post in allposts %}
    <div>
    {% for object in aa %}
    {% if object == post.id %}
    <p>{{m.object}}</p>
    {% endif %}
    {% endfor %}

   
    </div>
    {%endfor %}
 

but the problem is that I can't match the aa array and the m array in the HTML template but I can display {{m.1}} instead of {{m.object}} .但问题是我无法匹配 HTML 模板中的 aa 数组和 m 数组,但我可以显示{{m.1}}而不是{{m.object}} so how can I match those two arrays?那么我该如何匹配这两个 arrays 呢?

Im not sure what you mean by 'match' and 'arrangement' here exactly.我不确定这里的“匹配”和“安排”是什么意思。 If i dont answer your question then please elaborate on what you are trying to accomplish.如果我不回答您的问题,请详细说明您要完成的工作。

the problem is that I can't match the aa array and the m array in the HTML template问题是我无法匹配 HTML 模板中的 aa 数组和 m 数组

This is vague but suggests you want to attach certain elements in m to certain elements in aa.这是含糊的,但建议您要将 m 中的某些元素附加到 aa 中的某些元素。 I would suggest sending the data in a combined fashion like a dictionary我建议像字典一样以组合方式发送数据

new_m = {a: m[a] for a in aa}

in index.html I want to show 'like' or 'unlike' for each post according to the arrangement in the array在 index.html 我想根据数组中的排列为每个帖子显示“喜欢”或“不喜欢”

This is also a bit vague but suggests you want to order m according to aa.这也有点模糊,但建议您要根据 aa 订购 m。 Heres how you would do that:下面是你将如何做到这一点:

m = [x for x,_ in sorted(zip(m, aa)]

Lastly, Im even more confused by this line in the template: {% if object == post.id %} because all it would do is only display your like, unlike, and other options on the first 5 posts.最后,我对模板中的这一行更加困惑: {% if object == post.id %}因为它只会在前 5 个帖子上显示您的喜欢、不同和其他选项。

There are several ways to do it.有几种方法可以做到这一点。 I would filter your results with the list of IDs and sort it so it has the same order as your aa list.我将使用 ID 列表过滤您的结果并对其进行排序,使其与您的aa列表具有相同的顺序。 Then go with zip .然后 go 和zip

def arrays(request):
    aa = [0, 1, 2, 3, 4]
    filtered_posts = posts.objects.filter(id__in=aa).order_by("id")
    m = ['empty', 'like', 'unlike', 'like', 'unlike']
    posts_m_mapping = zip(filtered_posts, m)

    return render(request,  "network/index.html" ,{'posts_m_mapping': posts_m_mapping})

And in your template, just iterate over posts_m_mapping :在您的模板中,只需遍历posts_m_mapping

{% for post, sentiment in posts_m_mapping %}
   ...
{%endfor %}

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

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