在pythonanywhere和python和flask中使用pythonanywhere无法进行条件测试

时间:2018-09-17 22:46:21

标签: python flask pythonanywhere

我简单的条件测试无法正常工作。

这是flask_app.py代码:

testing_conditional_args = []
testing_conditional_args.append("a")
testing_conditional_args.append("b")
testing_conditional_args.append("c")
@app.route("/testing_conditional/", methods=['POST'])
def testing_conditional():
    if request.method == "POST":
          return render_template("testing_conditional.html", testing_conditional_args=testing_conditional_args)
    return redirect(url_for('testing_conditional'))

这是html模板(testing_conditional.html):

var0 = {{testing_conditional_args[0]}} <br>
var1 = {{testing_conditional_args[1]}} <br>
var2 = {{testing_conditional_args[2]}} <br>
{% if var1 == "b" %}
    args var1 and "b" are equal<br>
{% endif %}
{% if var1 != "b" %}
    args var1 and "b" are not equal<br>
{% endif %}
{% if "a" != "b" %}
    args "a" and "b" are not equal<br>
{% endif %}
{% if "a" == "a" %}
    args "a" and "a" are equal<br>
{% endif %}

Web浏览器中的输出显示var1为“ b”,但条件测试不起作用。

var0 = a
var1 = b
var2 = c
args var1 and "b" are not equal
args "a" and "b" are not equal
args "a" and "a" are equal

它说var1和“ b”在看起来应该相等时并不相等。 为什么var1无法识别为“ b”?

1 个答案:

答案 0 :(得分:0)

条件测试使用set命令进行工作:

        {% set var0=testing_conditional_args[0] %}
        {% set var1=testing_conditional_args[1] %}
        {% set var2=testing_conditional_args[2] %}

        {% if var1 == "b" %}
            args var1 and "b" are equal<br>
            <div style="color:blue; font-size:200%; text-align:left" >"Conditional Formatting"</div>
        {% endif %}