2

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

1

1 Answer 1

4

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 %}
Sign up to request clarification or add additional context in comments.

1 Comment

so you cant change the var that was passed from flask, but thanks you for the tips, will sure try :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.