Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Variable from python (flask) to HTML in render template?

The web server works (python flask) but when I go to the website, where the value of animal should be (dog) it shows the variable name animal. (There is more to the code but this is the most simplistic version which is the same concept.

Let's say I have these lines of code in my python script running python flask.

animal = dog return render_template('index.html', value=animal) 

and in my HTML

<h3>I like the animal: {{ value }}<h3> 

but rather than displaying 'dog' it displays the variable name 'animal'. So how would I go about displaying the Python variable to HTML? Thank you

like image 764
ThePGT Avatar asked Jul 17 '17 16:07

ThePGT


People also ask

How do you pass variables into render template in Flask?

To pass variables from Python Flask to JavaScript, we can call render_template with the data argument with the dictionary of data we want to pass to the template. to call render_template with the template file name and the data set to the data dict with the data we want to pass to the template.

How do I pass a Python variable to HTML?

Solution. In email. html change {{userEmail}} to {userEmail} . Then you should be able to use Python string formatting to add the userEmail variable.


1 Answers

Shouldn't it be animal = 'dog'? If 'dog' is the value you want to see printed after "I like the animal:", this is what you need to do:

animal = 'dog' return render_template('index.html', value=animal) 
like image 90
Renato Byrro Avatar answered Oct 10 '22 16:10

Renato Byrro