Passing python to a template

There must be a way to do this ... but I can't find it.

If I pass one dictionary to the template as follows:

@app.route("/")
def my_route():
  content = {'thing':'some stuff',
             'other':'more stuff'}
  return render_template('template.html', content=content)

This works well in my template ... but is there any way that I can give up "content"., From

{{ content.thing }}

I feel like I've seen it before, but can't find it anywhere. Any ideas?

+4
source share
2 answers

Try

return render_template('template.html', **content)
+8
source

You need to use the operator **to pass the contentdict arguments as a keyword:

return render_template('template.html', **content)

This is actually the same as passing elements in dict arguments as a keyword:

return render_template(
    'template.html',
    thing='some stuff',
    other='more stuff',
)
+8
source

Source: https://habr.com/ru/post/1629279/


All Articles