【问题标题】:Syntax error when trying to make a comparison [duplicate]尝试进行比较时出现语法错误[重复]
【发布时间】:2018-01-20 16:09:27
【问题描述】:
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 '}'

【问题讨论】:

  • 为什么要把username 括在大括号中?你不能在“逻辑”代码中做,只有 if book.author == username 应该是你需要的

标签: python flask jinja2


【解决方案1】:

{{..}} 不需要,您没有在输出中输入值。只需直接与变量比较:

{% if book.author == username %}

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 2018-12-13
    • 2023-02-18
    • 2017-08-05
    • 2020-09-28
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多