Pyramid - 是否可以将我的 mako 模板渲染为可调用视图中的字符串?
我有一个可调用视图,它看起来类似于:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此处记录的 Pyramid API 直接调用
render
:http://docs.pylonsproject.org/projects/pyramid/1.0/api/renderers.html#pyramid.renderers.renderYou can call
render
directly using the documented Pyramid API here: http://docs.pylonsproject.org/projects/pyramid/1.0/api/renderers.html#pyramid.renderers.render我不确定我是否理解你的问题,但我认为你需要 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.