okoboko
okoboko

Reputation: 4482

Printing a Dictionary Value in Jinja

I'm passing a dictionary to a Jinja template like this:

locations = {1: "Baltimore, MD"}

I want the value to appear like this:

Baltimore, MD

But, when I print the value in my Jinja template, it looks like this:

{'Baltimore, MD'}

Here's my code (in the template):

<td class="col-md-2">{{ locations[check.location_id] | string }}</td>

I tried adding a [0] to the end of locations[check.location_id] and it didn't help.

What's wrong?

Upvotes: 0

Views: 7721

Answers (1)

Doobeh
Doobeh

Reputation: 9440

You need to provide a full example, as extrapolating from the information given, the code works fine. Here's an example.

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello_world():
    check = {'location_id': 1}
    locations = {1: "Baltimore, MD"}
    return render_template('example.html', locations=locations, check=check)

if __name__ == '__main__':
    app.run()

Then example.html:

{{ locations[check.location_id] }}

Shows:

Baltimore, MD

in the rendered template.

Upvotes: 3

Related Questions