尝试进行比较时出现语法错误

时间:2018-01-20 16:09:27

标签: python flask jinja2

return render_template('books.html', username=session['username'], mybooks=q)
<html>
  Welcome {{username}}

  {% for book in mybooks %}
    {% if book.author == {{username}}%}
         <p>Author: {{ book.author }}</p>
         <p>Edition: {{book.edition}}</p>
    {% endif %}
  {% endfor %}
</html>

当我尝试比较时,我不明白为什么会出现错误:

{% if book.author == {{username}}%}

错误消息:

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

1 个答案:

答案 0 :(得分:1)

不需要

{{..}},您没有在输出中添加值。只需将直接与变量

进行比较
{% if book.author == username %}