51

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

5
  • I'd need to see some more code. I believe that should work, unless dog == 'animal' Commented Jul 17, 2017 at 16:40
  • No for some reason it doesn't work it just displays 'animal' rather than 'dog' Commented Jul 17, 2017 at 17:02
  • I'd need to see where dog is defined. As you're showing it, dog is another variable. Unless you meant to type animal = 'dog'. If you edit your code with the whole view it would be easier to tell you. Because as it is, this is correct. Commented Jul 17, 2017 at 17:11
  • Is there a way I can get this in url_for parameter? with {{ value }} Commented Nov 6, 2019 at 12:31
  • A good way to apply this method is to create an input and use the value in the value attribute, as in the example. <input type="hidden" value="{{ value }}" id="animal"> Commented Jul 7, 2020 at 21:17

3 Answers 3

69

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way I can get this in url_for parameter? with {{ value }}
@Dev yes going along with this example, use {{ url_for('static', filename=value) }} assuming that value is set to your intended url destination
3

To pass one variable, simply provide it as a keyword argument to the render_template function:

render_template("index.html", name='test')

In the template, you can access the variable directly:

<h1>Hello, {{ name }}!</h1>

To pass multiple variables, create a dictionary containing the variables and provide it as keyword arguments:

context = {
    'name': name,
    'age': age
}

render_template("index.html", **context)

In the template, you can access the variables directly:

<h1>Hello, {{ name }}!</h1>
<p>You are {{ age }} years old.</p>

Comments

0

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.

1 Comment

Is there a way I can get this in url_for parameter? with {{ value }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.