簡體   English   中英

要檢查的GAE模板代碼是列表中的項目

[英]GAE template code to check is item in the list

如何使用“ in”語句檢查列表中的項目是否存在。 如果我使用:

{% for picture in pictures %}
   {% if picture in article.pictures %}
      <input type="checkbox" checked="true" name="picture" value="{{ picture.key }}"  />
   {% else %}
      <input type="checkbox" name="picture" value="{{ picture.key }}"  />
   {% endif %}
      <img src='/img?img_id={{ picture.key }}'></img> <br />
{% endfor %}

這是失敗的:

TemplateSyntaxError: 'if' statement improperly formatted

在線

{% if picture in article.pictures %}

救命?

默認情況下,Django模板不支持完整的條件表達式。 您可以使用if檢查一個值是否為“ true”,或者可以使用ifequal檢查兩個值是否相等。

渲染模板之前,也許可以在視圖中裝飾picture

for picture in pictures:
    picture.is_in_article = (picture in article.pictures)

然后,您可以在模板中對該新屬性的值進行操作。

{% for picture in pictures %}
    {% if picture.is_in_article %}
        <input type="checkbox" checked="true" name="picture" value="{{ picture.key }}"  />
    {% else %}
        <input type="checkbox" name="picture" value="{{ picture.key }}"  />
    {% endif %}
    <img src='/img?img_id={{ picture.key }}'></img> <br />
{% endfor %}

我尚未使用GAE,但可以在此處的補丁中找到自定義“ ifin” Django標記的代碼。 如我的評論所述,該功能似乎可以在Django 1.2中實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM