【问题标题】:Display a Boolean field result if it's True如果为 True,则显示布尔字段结果
【发布时间】:2021-07-21 18:36:55
【问题描述】:

我无法在模板中显示返回的布尔字段。如果在模板中为真,我需要显示该字段的名称。欢迎任何帮助。

模型

''' IEIS 类(Model.models): topo = models.BooleanField() '''

观看次数

def view_ieis(request, pk):
    ieis = IEIS.objects.get(pk=pk)
    return render(request, 'ieis/view.html', {'ieis': ieis,})

template.html

<div table class="table table-reponsive">
    <table class="table table-bordered">
      <thead>
        <tr>

          <th scope="col">Tipo</th>

        </tr>
      </thead>
      <tbody>
        <tr>
                <td>
                    {% if topo is True %}
                        <p>Topo</p>
                    {% endif %}

           </td>
        </tr>
      </tbody>
    </table>
</div>

【问题讨论】:

    标签: django django-templates boolean templatetags


    【解决方案1】:

    您将IEIS 对象作为ieis 传递给模板,这意味着您可以检查该对象的布尔内容:

    {% if ieis.topo %}
        <p>Topo</p>
    {% endif %}

    【讨论】:

    • 对。谢谢你的帮助,威廉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多