问题
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
回答1:
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)
回答2:
As you have to pass a value using a variable you have to define it as a string like:
animal = 'dog'
then pass it to the html template:
return render_template('index.html', value=animal)
Now, you should see the passed value in the HTML.
来源:https://stackoverflow.com/questions/45149420/pass-variable-from-python-flask-to-html-in-render-template