【问题标题】:Display QuerySet result as Array将 QuerySet 结果显示为数组
【发布时间】:2016-11-28 15:29:30
【问题描述】:

我在问一个关于我的 HTML 模板的问题。我用 Django 显示了一些 QuerySet 的结果,我想修改显示用户方面。

我不知道我是否可以用 Django 制作这种东西,或者 JavaScript 是否可以制作。

我有一个 HTML 模板:

<h2 align="center"> Affichage de toutes les fiches individuelles </align> </h2>

<br></br>

{% block content %}

<h4> Récapitulatif des 10 dernières fiches individuelles créées: </h4>
<ul>
{% for item in identity %}
   <li>{{ item }}</li>
{% endfor %}
</ul>

<h4> Récapitulatif des 10 dernières fiches individuelles créées habitant en France: </h4>
<ul>
{% for item in identity_France %}
   <li>{{ item }}</li>
{% endfor %}
</ul>

{% endblock %}

从这个角度来看:

def Consultation(request) :

    identity = Identity.objects.all().order_by("-id")[:10] #Les 10 dernières fiches créées
    identity_France = Identity.objects.filter(country='64').order_by("-id")[:10] #Les 10 dernières fiches où la personne habite en France

    context = {
        "identity" : identity,
        "identity_France" : identity_France,
    }
    return render(request, 'resume.html', context)

是否可以将结果显示为数组?使用列,每列的标签等...?

类似的东西:

谢谢!

编辑:

我发现 JQuery 可以做到这一点:JQuery Array

【问题讨论】:

    标签: javascript django


    【解决方案1】:

    你可以在 {% for %} {% endfor %} 模板标签中做任何你想做的事情。 比如,你可以把你的对象放在一张桌子里

    <table>
      <thead>
         <tr>
           <th>Field 1 Name</th>
           <th>Field 2 Name</th>
         </tr>
       </thead>
       <tbody>
       {% for item in identity_France %}
          <tr>
            <td>{{ item.field1 }}</td>
            <td>{{ item.field2 }}</td>
          <tr>
       {% endfor %}
       <tbody>
    </table>
    

    【讨论】:

    • 是否可以先显示第一行作为字段名然后显示对象?
    • 是的,我找到了这个 HTML 命令 ;) 如果我想显示数组行,它是用 javascript 的?
    • 我不明白你说的“数组行”是什么,你能给我举个例子吗?
    • 我的意思是绘制一个数组。绘制所有数组轮廓?
    • 你可以使用{%里面的forloop.counter
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2020-07-05
    • 1970-01-01
    • 2010-12-18
    • 2013-05-28
    • 2019-07-19
    • 2022-06-19
    相关资源
    最近更新 更多