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

落花浮王杯 提交于 2021-02-05 20:54:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!