将 HTMLFill 与 Pyramid 的 @view_config 结合使用

发布于 2024-12-20 09:01:07 字数 1206 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-12-27 09:01:07

您可以将表单放置在单独的 mako 文件中,例如 form.mako。从那里您可以将其渲染为字符串,通过 htmlfill 传递它并将结果传递给您的实际模板。

<!-- 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}

您需要通过 n 过滤器过滤 form_html,以避免渲染的 html 被转义。

当然,您始终可以将所有内容放在同一个模板中并渲染它,从您的视图返回一个 Response 对象并绕过渲染器。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文