学习金字塔(python)并且正在与@view_config装饰器斗争。它应该开箱即用吗?
我仍在学习金字塔,并且我正在尝试学习如何使用装饰器。下面是我的可调用测试视图的副本。
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.renderers import render_to_response
def my_blog(request):
return {'project':'tricky'}
@view_config( renderer='templates/foo.pt' )
def foo_blog(request):
return {'name':'tricky'}
根据我对 view_config 装饰器的理解,它可以用于设置应用程序配置,而无需在配置文件中实际设置它们。在此示例中,我将渲染器设置为 templates/foo.pt。这永远行不通。
但是,如果我在配置文件(init.py)中设置渲染器:
config.add_route( 'foo_blog' , '/blog/{foo}' , view='tricky.views.Blog.blog.foo_blog' renderer='tricky:templates/mytemplate.pt' )
它将起作用。
我是否做错了什么,导致我无法使用装饰器。谢谢!
I am still learning pyramid, and I am at a point where I am trying to learn how to use decorators. Below is a copy of my test view callable.
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.renderers import render_to_response
def my_blog(request):
return {'project':'tricky'}
@view_config( renderer='templates/foo.pt' )
def foo_blog(request):
return {'name':'tricky'}
From what I can understand about the view_config decorator, it can be used to set application configurations without actually setting them in the config file. In the case of this example, I am setting the renderer to be templates/foo.pt. This does not ever work.
However, if I set renderer in the config file (init.py) as such:
config.add_route( 'foo_blog' , '/blog/{foo}' , view='tricky.views.Blog.blog.foo_blog' renderer='tricky:templates/mytemplate.pt' )
it will work.
Am I doing something wrong that is preventing me from being able to use the decorator. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使通过 @view_config 添加的配置起作用,您需要在某个时候调用 config.scan() 。
In order for the configurations added via @view_config to work, you need to call config.scan() at some point.