[Flask] Passing dict instead of Context in render_template

Xan xancorreu at gmail.com
Wed Feb 3 08:01:17 EST 2016


Oh!!! Thanks a lot. To all of you.
Ridiculous simple! ;-)

On Wed, 3 Feb 2016 07:05:56 -0500
David Baumgold <david at davidbaumgold.com> ha escrit:

> 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  


More information about the Flask mailing list