簡體   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