Skip to content Skip to sidebar Skip to footer

How Can I Pass A Python Variable To My Template Via Flask?

I made a web application with flask. I want to pass a varable value to my template, to set property. in python: web_size='100%' @app.route('/') def home(): return render_templat

Solution 1:

To pass a Python variable to a Flask template do the following:

In the view first declare the Python variable. Then, pass the variable to the template as a parameter in your render_template method:

@app.route('/', methods={'GET'})defhome():
    title = "Frankenstein"return render_template('index.html', book_title=title )

Then, to display the variable in your index.html template use curly braces:

<h3>The book named "{{book_title}}" </h3>

Solution 2:

You haven't passed the data into the html sheet. For example:

message = 'Hello!'return render_template('index.html', value= message)

Post a Comment for "How Can I Pass A Python Variable To My Template Via Flask?"