开发者

GAE template code to check is item in the list

How to开发者_运维问答 use "in" statement to check is item in the list or not. If I use:

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

this is failing with:

TemplateSyntaxError: 'if' statement improperly formatted

on line

{% if picture in article.pictures %}

help?


By default, Django templates do not support full conditional expressions. You can check if one value is "true" with if, or you can check whether two values are equal with ifequal, etc.

Perhaps you can decorate your pictures in the view before you render the template.

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

Then in the template you can act on the value of that new attribute.

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


I've not worked with GAE, but the code for a custom "ifin" Django tag can be found in the patch here. As mentioned in my comment, it looks like that functionality may be implemented in Django 1.2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜