[Flask] Passing dict instead of Context in render_template

Alejo Arias alejoar at gmail.com
Wed Feb 3 06:09:33 EST 2016


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160203/3ed46acc/attachment.html>


More information about the Flask mailing list