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 picture
s 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
精彩评论