Pyramid - 是否可以将我的 mako 模板渲染为可调用视图中的字符串?

发布于 2024-11-14 20:09:16 字数 802 浏览 1 评论 0原文

我有一个可调用视图,它看起来类似于:

def post_comment(request):
    """ Posts the users comment to the thread """

    try:
        new_comment = comments.post()
    except InvalidComment as e:
        return {'success' : False, 'message' : e.message}

    # need to do something like:
    new_comment = pyramid.template.render(new_comment)

    return {'success' : True, 'message' : new_comment}

该可调用视图的路由配置是:

config.add_route('post_comment',
                 '/comments/{link_id}/post',
                 view='site.views.post_comment',
                 view_renderer='json')

使用这个,我可以 AJAX 化我的评论提交并拥有一个闪亮的 Web 2.0 网站。问题是,我想通过我的 mako 模板渲染 new_comment 来构建 HTML 并返回它。但是,我找不到办法做到这一点。

如何在我的视图中渲染 mako 模板,可调用以将 HTML 作为 JSON 响应返回?

I have a view callable that does looks similar to:

def post_comment(request):
    """ Posts the users comment to the thread """

    try:
        new_comment = comments.post()
    except InvalidComment as e:
        return {'success' : False, 'message' : e.message}

    # need to do something like:
    new_comment = pyramid.template.render(new_comment)

    return {'success' : True, 'message' : new_comment}

The route config for this view callable is:

config.add_route('post_comment',
                 '/comments/{link_id}/post',
                 view='site.views.post_comment',
                 view_renderer='json')

Using this, I can AJAXify my comment submissions and have a shiny web 2.0 website. The problem is, I'd like to render new_comment through my mako template to build the HTML and return that. However, I can't find a way to do this.

How can I render a mako template within my view callable to return the HTML as a JSON response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

空心空情空意 2024-11-21 20:09:16

您可以使用此处记录的 Pyramid API 直接调用 renderhttp://docs.pylonsproject.org/projects/pyramid/1.0/api/renderers.html#pyramid.renderers.render

from pyramid.renderers import render

def my_view(request)
    renderer_dict = {} # dictionary of values to pass to the renderer
    new_comment = render('new_comment.mako', renderer_dict, request)
    …

You can call render directly using the documented Pyramid API here: http://docs.pylonsproject.org/projects/pyramid/1.0/api/renderers.html#pyramid.renderers.render

from pyramid.renderers import render

def my_view(request)
    renderer_dict = {} # dictionary of values to pass to the renderer
    new_comment = render('new_comment.mako', renderer_dict, request)
    …
尛丟丟 2024-11-21 20:09:16

我不确定我是否理解你的问题,但我认为你需要 2 个视图,一个用于 json,另一个用于 mako 视图。对于 mako 视图,请遵循官方文档< /a> 和默里克尔的回答。如果您想在同一个类中拥有多个视图可调用对象,您可以查看 pyramid_handlers

I'm not sure I understand your question but I think you need 2 views, one for json and another for the mako one. For the mako view follow the official docs and mmerickel's answer. If you want to have multiple view callables within the same class you can have a look to pyramid_handlers.

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