-1

My simple conditional test is not working.

Here is the flask_app.py code:

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'))

Here is the html template (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 %}

The output in the web browser shows that var1 is "b", but the conditional test does not work.

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

It says that var1 and "b" are not equal when it seems they should be. Why is var1 not recognized as "b"?

R. Wayne
  • 417
  • 1
  • 9
  • 17
  • First of alll, you can not do variable assignments in template like normal python. Second, you would like to look at this [post](https://stackoverflow.com/questions/1070398/how-to-set-a-value-of-a-variable-inside-a-template-code) – yogkm Sep 17 '18 at 22:49
  • Yes, I knew it wasn't the correct way to assign variables and your suggested post showed me how to do it correctly: {% set var0=testing_conditional_args[0] %} {% set var1=testing_conditional_args[1] %} {% set var2=testing_conditional_args[2] %} Now it works the way I wanted!! – R. Wayne Sep 18 '18 at 22:41
  • To give you credit for answering the question you need to have an answer in addition to your comment. – R. Wayne Sep 18 '18 at 22:43

1 Answers1

0

The conditional test works using the set command:

        {% 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 %}
R. Wayne
  • 417
  • 1
  • 9
  • 17