2009-12-24 3 views
0

"in"문을 사용하여 확인하는 방법은 목록의 항목인지 확인하십시오. 내가 사용하는 경우 : 라인확인할 GAE 템플릿 코드가 목록의 항목입니다

TemplateSyntaxError: 'if' statement improperly formatted 

{% if picture in article.pictures %} 

도움이 함께 실패

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

?

+0

Django 템플릿이 Django 1.2 (http://code.djangoproject.com/ticket/8087)에있는 것처럼 보입니다. 그래도 그게 도움이 될지 모르겠다. –

답변

2

기본적으로 장고 템플릿은 전체 조건식을 지원하지 않습니다. 하나의 값이 "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 %} 
1

저는 GAE를 사용하지는 않았지만 커스텀 "ifin"Django 태그의 코드는 here 패치에서 찾을 수 있습니다. 내 의견에서 언급했듯이, 그 기능은 장고 1.2에서 구현 될 수있는 것 같습니다.

+0

감사합니다. GAE에서 사용할 수 있습니까? –

관련 문제