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

2024/11/20 3:20:32

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

Answer

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)
https://en.xdnf.cn/q/26361.html

Related Q&A

How to clear console in sublime text editor

How to clear console in sublime text editor. I have searched on internet too..But cant find proper shortcut for that. Please provide info

YAML loads 5e-6 as string and not a number

When I load a number with e form a JSON dump with YAML, the number is loaded as a string and not a float.I think this simple example can explain my problem.import json import yamlIn [1]: import jsonIn …

How to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y

Im plotting two data series with Pandas with seaborn imported. Ideally I would like the horizontal grid lines shared between both the left and the right y-axis, but Im under the impression that this is…

How to set the line width of error bar caps

How can the line width of the error bar caps in Matplotlib be changed?I tried the following code:(_, caplines, _) = matplotlib.pyplot.errorbar(data[distance], data[energy], yerr=data[energy sigma],cap…

Where is Pythons shutdown procedure setting module globals to None documented?

CPython has a strange behaviour where it sets modules to None during shutdown. This screws up error logging during shutdown of some multithreading code Ive written.I cant find any documentation of this…

Save a dictionary to a file (alternative to pickle) in Python?

Answered I ended up going with pickle at the end anywayOk so with some advice on another question I asked I was told to use pickle to save a dictionary to a file. The dictionary that I was trying to sa…

python equivalent of functools partial for a class / constructor

I want to create a class that behaves like collections.defaultdict, without having the usage code specify the factory. EG: instead of class Config(collections.defaultdict):passthis:Config = functools.p…

Set the font size in pycharms python console or terminal

There are terminal and python console in pycharm, which are very convenient. But I found that the font size was too small to recognize in terminal or python console. How can change the font size in the…

How to filter model results for multiple values for a many to many field in django

I have the following Model:class Group(models.Model):member = models.ManyToManyField(Player, through=GroupMember)name = models.CharField(max_length=20, unique=True)join_password = models.CharField(max_…

Why is string comparison so fast in python?

I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem:Given two strings, return the length of the longest comm…