I'm using Chameleon in Python to render my templates.
Let's say I want to populate a form field using POST/GET data:
<input type="text" name="foo" value="${request.params['foo']}" />
The problem with that is if request.params has no key "foo", I get an error. What's the simplest way to have value="" be empty if the key doesn't exist, rather than throwing an error?
request.params
is a dict (or dict-like) object, so you can just use .get()
with a default value:
<input type="text" name="foo" value="${request.params.get('foo', '')}" />