Django模板:检查空查询集

[英]Django template: check for empty query set


Is there a way to check for an empty query set in the Django template? In the example below, I only want the NOTES header to be displayed if there are notes.

是否有方法检查Django模板中的空查询集?在下面的示例中,我只希望在有NOTES时显示NOTES头部。

If I put an {% empty %} inside the "for" then it does display whatever is inside the empty tag, so it knows it's empty.

如果我在“for”中放入{% empty %},那么它将显示空标记内的任何内容,因此它知道空标签是空的。

I'm hoping for something that does not involve running the query twice.

我希望找到不需要运行查询两次的东西。

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

Clarification: the above example "if notes" does not work - it still displays the header even with an empty query set.

澄清:上面的例子“如果注释”不工作——它仍然显示头,即使是空的查询集。

Here's a simplified version of the view

这是视图的简化版本

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))  

Edit: the view select selects from multiple tables.

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

6 个解决方案

#1


11  

Try {% if notes.all %}. It works for me.

如果笔记尝试{ %。% }。它适合我。

#2


10  

Have a look at the {% empty %} tag. Example from the documentation

查看{% empty %}标签。文档的例子

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

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

链接:https://docs.djangoproject.com/en/1.8/ref/templates/builtins/ #为空

#3


4  

In your view check whether notes is empty or not. If it is then you pass None instead:

在您的视图中检查notes是否为空。如果是的话,你就不及格:

{"notes": None}

In your template you use {% if notes %} as normal.

在模板中,使用{% if notes %}作为常规。

#4


4  

It's unfortunate that you're stuck using a raw query set - they're missing a lot of useful behavior.

不幸的是,您使用的是原始查询集——它们缺少许多有用的行为。

You could convert the raw query set into a list in the view:

您可以将原始查询设置为视图中的列表:

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

Then check it as a boolean in the template:

然后在模板中以布尔形式检查:

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

You could also make it happen without conversions using forloop.first:

你也可以不使用forloop进行转换。

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

#5


1  

What about:

是什么:

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

#6


0  

Your original solution

你原来的解决方案

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

Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.

现在可以使用Django 1.7,多亏了QuerySet缓存,它不需要花费额外的查询。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/07/02/976034087a058a8c341a57468929347f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告