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:
Baca Juga
- How Can I Automate The Form Filling Process For A User On My Webpage With Voice Via Their Microphone?
- What Is The Best Way To Implement A Forced Page Refresh Using Flask?
- In A Flask Function Which Returns `send_file`, The Code Doesn't Appear To Run On Subsequent Requests, Yet The File Still Downloads. Why?
<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?"