简体   繁体   中英

How can I make certain HTML not show up using Flask and Jinja2?

I'm making a Flask app that takes a single input from a user, and then uses that input to query an API, and return the results.

I'm trying to use the same template for both getting the user input, and displaying the results.

My return render template looks like this:

return render_template("query.html", json_data=json_data, info=info)

The problem is that when the page first loads, it's looking for the json_data , and info variables, but they don't exist yet.

I tried doing this:

data = request.args.get("t")

    if data:
        ...
        return render_template("query.html", json_data=json_data, info=info)

    else:

        return render_template("meter_mdm.html", json_data=None, info=None)

And then in my Jinja template, I put:

{% if json_data is not none and info is not none %}   

    ...HTML that uses the json_data and info variables

{% endif %}

But it's still loading the data between the if statement.

Any idea what I need to do to load the results on the same page?

Try to simplify this line:

{% if json_data is not none and info is not none %}   

to:

{% if json_data and info %}

Here is a worked demo:

view:

app.route('/')
def index():
    name = None
    age = None
return render_template('index.html', name=name, age=age)

index.html:

{% if name and age %}
Hello, boy!
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM