Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add single if statement in flask

I am using Python 3.5 to develop a web app with Flask. Having the following code:

from flask import Flask, render_template


app = Flask(__name__)

@app.route('/')
def main():
    return render_template('main.html', show_hidden=False)

app.run()

and the template main.html:

<html>
  <body>
    <h1>Hey!</h1>
    {% if show_hidden == True %}
        <p>Hidden text</p>
    {% endif %}
  </body>
</html>

Right now this seems an unuseful feature, but I'll find a way to determine whether the hidden text should be displayed.

When the above code runs, the hidden text is shown although show_hidden is set to False. Furthermore, when it is turned to True the hidden text is displayed twice. As I have followed the docs this code is supposed to be working. Could someone please point me out what am I missing?

like image 329
james kaly Avatar asked Oct 21 '25 01:10

james kaly


1 Answers

The == True is redundant, you can just check whether {% if show_hidden %}

Change :

{% if show_hidden == True %}

To :

{% if show_hidden %}
like image 69
omri_saadon Avatar answered Oct 22 '25 16:10

omri_saadon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!