geogeogeo
geogeogeo

Reputation: 381

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?

Upvotes: 1

Views: 2811

Answers (1)

Grey Li
Grey Li

Reputation: 12772

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 %}

Upvotes: 5

Related Questions