[Flask] Passing dict instead of Context in render_template
David Baumgold
david at davidbaumgold.com
Wed Feb 3 07:05:56 EST 2016
It would be simpler to use Python’s built-in argument list unpacking: https://docs.python.org/2/tutorial/controlflow.html#tut-unpacking-arguments
You can do something like this:
context = {‘foo’: 2, ‘baa’: 3}
return render_template(template, **context)
On February 3, 2016 at 6:10:25 AM, Alejo Arias (alejoar at gmail.com) wrote:
Hi Xan,
Did you try render_template(template, a=a)?
You can then access your dict values from the template like so: {{ a['foo'] }}
If 'foo' is a key that might not exist, you can use {{ a.get('foo', 'default result') }}. This will print 'default result' if foo doesn't exist.
Regards,
Alejo
On 3 February 2016 at 11:53, Xan <xancorreu at gmail.com> wrote:
Hi,
I just want to know if I can do something like render_template(template, {'foo': 2, 'baa': 3}) instead of render_template(template, foo=2, baa=3). I want to pass a variable list of assignment. So I think that dict is the only solution.
If I have:
a = {'foo': 2, 'baa': 3, ...}
How can I do, render_template(template, a)? (a has variable value)
Thanks a lot,
Xan
PS: I asked in stackoverflow with a missunderstanding http://stackoverflow.com/questions/35117821/pass-a-list-of-assignments-in-a-template-in-a-flask
_______________________________________________
Flask mailing list
Flask at python.org
https://mail.python.org/mailman/listinfo/flask
_______________________________________________
Flask mailing list
Flask at python.org
https://mail.python.org/mailman/listinfo/flask
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160203/058318c1/attachment.html>
More information about the Flask
mailing list