I am trying to use HTMLFill to populate a form with default values. I have a function that uses the @view_config decorator and generates a form in mako.
@view_config(renderer="templates/derived/new/model.mak", route_name='new_model_route')
def new_model(self, fparams=None):
defaults = {'node_name': 'blah'}
process_route = route_url("process_model_route", self.request, ppath=ppath)
return dict({'ppath':ppath, 'process_route':process_route})
Template:
<%def name="direct_load_model_form(method, target_path)">
${h.tags.form(method, multipart=True, method='post', hidden_fields=[('ppath', ppath)])}
<b>Node Name: </b>${h.tags.text('node_name')}<BR>
<b>Parameters: </b>${h.tags.file('params_file')}<BR>
${h.tags.submit('submit', 'Submit')}
${h.tags.end_form()}
</%def>
${self.direct_load_model_form(process_route, ppath)}
The basic usage example looks like:
>>> from formencode import htmlfill
>>> form = '<input type="text" name="fname">'
>>> defaults = {'fname': 'Joe'}
>>> htmlfill.render(form, defaults)
'<input type="text" name="fname" value="Joe">'
I am not use how to populate the first argument of htmlfill.render
in my case.
You can place the form in a separate mako file like
form.mako
. From there you can render it to a string, pass it throughhtmlfill
and pass the result to your actual template.You'll need to filter the form_html via the
n
filter to avoid the rendered html being escaped.Of course you could always put everything in the same template and render it instead, returning a
Response
object from your view and bypassing the renderer.