I'm using Pyramid 1.3 with Mako Templating. I have a view with a method called "create" that returns an empty dictionary to a template. I intend to use the same "create.mako" template for both creating and editing. I've put the context variable like such in my input fields:
<input type="text" id="nameInput" value="${content['name']}" />
The problem with this is that I get errors like below when I try to load the create method:
<input id="nameInput" value="${content['name']}" type="text" class="span8" style="background-color: #EED3D7;" />
TypeError: 'Undefined' object is unsubscriptable
This is of course correct because the create method only returns an empty dictionary and so does not have the key "content". This used to be fine in Pylons 0.9.7; it would just be an empty string if it did not exist.
I found a solution from StackOverflow:
import mako.runtime
mako.runtime.UNDEFINED = ''
But where do I put this in my Pyramid project?
Why not return the same dict to the template every time? That is (after all) the expected input of your template. It will really help in avoiding potential errors in your templates if you don't silently ignore issues. You can, of course, abstract these things across views.
def _create_tmpl(name='', ...):
return {
'name': name,
}
def create_view(request):
return _create_tmpl()
def edit_view(request):
return _create_tmpl(name='Mark')
Anyway, if you really really really want to, you can just add to your settings (probably in your INI) mako.strict_undefined = false
.