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 through htmlfill
and pass the result to your actual template.
<!-- new_model_form.mako -->
<form ...>
</form>
from pyramid.renderers import render
@view_config(..., renderer="new_model.mako")
def new_model(self):
raw_form_html = render('new_model_form.mako', request, {...})
form_html = htmlfill(raw_form_html, ...)
return {
'form_html': form_html,
}
<!-- new_model.mako -->
<% inherit file='base.mako' %>
${form_html | n}
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.
from pyramid.renderers import render
@view_config(...) # no renderer needed
def new_model(self):
raw_html = render('new_model.mako', request, {...})
html = htmlfill(raw_html, ...)
resp = request.response
resp.body = html
return resp