将 HTMLFill 与 Pyramid 的 @view_config 结合使用
我正在尝试使用 HTMLFill 用默认值填充表单。我有一个使用 @view_config 装饰器并在 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})
模板:
<%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)}
基本用法示例如下:
>>> from formencode import htmlfill
>>> form = '<input type="text" name="fname">'
>>> defaults = {'fname': 'Joe'}
>>> htmlfill.render(form, defaults)
'<input type="text" name="fname" value="Joe">'
在我的例子中,我不使用如何填充 htmlfill.render
的第一个参数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将表单放置在单独的 mako 文件中,例如
form.mako
。从那里您可以将其渲染为字符串,通过htmlfill
传递它并将结果传递给您的实际模板。您需要通过
n
过滤器过滤 form_html,以避免渲染的 html 被转义。当然,您始终可以将所有内容放在同一个模板中并渲染它,从您的视图返回一个 Response 对象并绕过渲染器。
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.