繁体   English   中英

Django 模板:检查空查询集

[英]Django template: check for empty query set

有没有办法检查 Django 模板中设置的空查询? 在下面的示例中,如果有注释,我只希望显示 NOTES 标题。

如果我在“for”中放了一个 {% empty %} 那么它会显示空标签内的任何内容,所以它知道它是空的。

我希望不涉及两次运行查询的东西。

{% if notes - want something here that works %}
     NOTES:
     {% for note in notes %}
         {{note.text}}  
     {% endfor  %}
{% endif  %}

说明:上面的示例“if notes”不起作用 - 即使查询集为空,它仍然显示标题。

这是视图的简化版本

sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))

return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))  

编辑:视图选择从多个表中选择。

看看{% empty %}标签。 文档中的示例

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>

链接: https : //docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty

尝试{% if notes.all %} 这个对我有用。

在您的视图中检查notes是否为空。 如果是,那么你通过None代替:

{"notes": None}

在您的模板中,您可以正常使用{% if notes %}

不幸的是,您在使用原始查询集时陷入困境 - 他们错过了许多有用的行为。

您可以将原始查询集转换为视图中的列表:

notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))

然后在模板中将其检查为布尔值:

{% if notes %}
    Header
    {% for note in notes %}
        {{ note.text }}
    {% endfor %}
{% endif %}

您也可以使用forloop.first无需转换forloop.first

{% for note in notes %}
    {% if forloop.first %}
         Header
    {% endif %}
    {{ note.text }}
{% endfor %}

关于什么:

{% if notes != None %}
    {% if notes %}
        NOTES:
        {% for note in notes %}
            {{ note.text }}  
        {% endfor  %}
    {% endif %}
{% else %}
    NO NOTES AT ALL
{% endif %}

您的原始解决方案

{% if notes %}
    Header
    {% for note in notes %}
        {{ note.text }}
    {% endfor %}
{% endif %}

现在可与 Django 1.7 一起使用,并且由于 QuerySet 缓存,它不需要成本和额外的查询。

有没有办法检查Django模板中设置的空查询? 在下面的示例中,我只希望在有注释的情况下显示NOTES标头。

如果我将{%empty%}放在“ for”内,则它会显示empty标记内的内容,因此它知道它是空的。

我希望不涉及两次运行查询的事情。

{% if notes - want something here that works %}
     NOTES:
     {% for note in notes %}
         {{note.text}}  
     {% endfor  %}
{% endif  %}

说明:上面的示例“ if notes”不起作用-即使查询集为空,它仍然显示标题。

这是视图的简化版本

sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))

return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))  

编辑:视图选择从多个表中选择。

通常正确的方法是使用{% with ... %}标签。 这会缓存查询,因此它只运行一次,并且与使用{% empty %}相比,还为您的标记提供了更大的灵活性。

{% with notes as my_notes %}
{% if my_notes %}
<ul>
  {% for note in my_notes %}
  <li>{{ note }}</li>
  {% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}

对于这个特定的例子,我不确定它有多大用处,但例如,如果您正在查询多对多字段,这很可能是您想要做的。

在 Django 模板中使用 {% empty %}

{% if list_data %}
    {% for data in list_data %}
        {{ data.field_1 }}
    {% endfor %}
{% else %}
    <p>No data found!</p>
{% endif %}

我们可以用 {% empty %} 编写上面的代码。

{% for data in list_data %}
    {{ data.field_1 }}
{% empty %}
    <p>No data found!</p>
{% endfor %}

暂无
暂无

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

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