【问题标题】:GAE template code to check is item in the list要检查的 GAE 模板代码是列表中的项目
【发布时间】:2009-12-24 23:11:14
【问题描述】:

如何使用“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 %}

帮助?

【问题讨论】:

标签: python google-app-engine templates


【解决方案1】:

默认情况下,Django 模板不支持完整的条件表达式。可以用if判断一个值是否为“真”,也可以用ifequal判断两个值是否相等等

也许您可以在渲染模板之前在视图中装饰您的pictures。

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 %}

【讨论】:

    【解决方案2】:

    我没有使用过 GAE,但是可以在补丁 here 中找到自定义“ifin”Django 标记的代码。正如我在评论中提到的,看起来该功能可以在 Django 1.2 中实现

    【讨论】:

    • 谢谢,我看看能不能用在 GAE 上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多