[英]If statement on for statement in Jinja2
我有一個在GAE中托管的網站,在Jinja2模板中,我在一個FOR語句中有一個IF語句。
我使用以下命令啟用了jinja2.ext.loopcontrols循環控件:
template_dir = os.path.dirname(__file__)
ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True,
extensions=['jinja2.ext.autoescape',
'jinja2.ext.loopcontrols'])
我的模板如下所示:
{% for i in data %}
(% if i.date_posted is defined %)
{{ i.date_posted.strftime('%d %b %Y') }}
{% else %}
No
{% endif %}
{% endfor %}
我不斷收到錯誤:
TemplateSyntaxError:遇到未知標簽“ endif”。 Jinja正在尋找以下標簽:'endfor'。 需要關閉的最里面的塊是“ for”。
您沒有正確聲明if
標簽:
(% if i.date_posted is defined %)
注意括號而不是花括號; 它應該這樣寫:
{% if i.date_posted is defined %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.