Reputation: 35
I am making a web application with flask, I know for a fact that you can create variables in the python file to use in the HTML file in jinja, but how can I change the value of a variable inside the HTML?
Can I do it like this?
{{ VAR_NAME == NEW_VAR_VALUE }}
help would be appreciated
Upvotes: 2
Views: 7850
Reputation: 543
Given a flask code like this:
@app.route('/')
def simple_page():
random_number = random.randint(1, 6)
return render_template('page.html', number=random_number)
Then you can access that variable through jinja2 like this:
<p>Random number: {{ number }}</p>
If you want to assign the variable passed from flask to another variable created in jinja2 (you wanna make a copy, I don't know) just write:
{% set new_var = number %}
Now you can use the variable new_var
in the jinja2 code.
Wanna increase new_var
? Do:
{% set new_var = new_var+1 %}
Upvotes: 4