金字塔处理程序不工作
我目前正在将一个项目从 Pylons 1.0 迁移到 Pyramid。
到目前为止我的问题是如何在金字塔中使用静态路由。我目前正在使用金字塔处理程序,因为它似乎是一个好的开始。我正在使用阿赫特。
因此,这是我的路线中的两行重要内容:
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts")
# or
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts", action="new")
我的操作:
@action(name="new_account", renderer='accounts/new.mako', request_method='GET')
错误:
TypeError: 'Accounts' object is not callable
or
ValueError: Could not convert view return value "{}" into a response Object.
帐户...到目前为止一切顺利,很容易理解金字塔_处理程序似乎没有正常注册或处理名称,因为它应该...在 request.matched_route
中说,我确实有“new_account”。
如果我在路由定义中添加“action='new'”,它将找到该函数,但不会侦听操作定义。换句话说,它将无法找到渲染器并期望响应对象。 request_method
参数实际上还没有执行任何操作,因此删除它不会改变任何结果。
简而言之,@action(name="..."
不起作用。Pyramid 无法自行找到该函数,如果定义了函数名称,则无法执行操作语句。
否知道我做错了什么
。
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts", action="new_account")
EDIT
route_name
可能会被 url 生成器函数使用。据我了解,@action name 是route_name,而不是action。现在这个名字更有意义了。
I'm currently moving a project from Pylons 1.0 to Pyramid.
My problem so far is how to use restful routes in Pyramid. I'm currently using pyramid_handlers since it seemed like a good start. I'm using Akhet.
So here is the two important lines in my route:
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts")
# or
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts", action="new")
My action:
@action(name="new_account", renderer='accounts/new.mako', request_method='GET')
The errors:
TypeError: 'Accounts' object is not callable
or
ValueError: Could not convert view return value "{}" into a response Object.
Accounts... so far so good, it is easy to understand that pyramid_handlers doesn't seem to register normally or handle name as it should... that said in request.matched_route
, I do have "new_account".
If I add "action='new'" in the route definition, it will find the function but it will not listen to the action definition. In other words, it will fail to find a renderer and expect a response object. The request_method
parameter doesn't actually do anything yet, so removing it doesn't change any results.
In short, the @action(name="..."
doesn't work. Pyramid fails to find the function by itself and if the function name is defined it fails to execute the action statement.
No idea what I'm doing wrong.
Correct way to do it.
config.add_handler("new_account", "/accounts/new", "sproci2.handlers.accounts:Accounts", action="new_account")
EDIT
route_name
is probably going to get used by url generator functions. While action is the actual name in @action. As I understood, @action name was the route_name and not the action name. That makes more sense now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对
add_handler
的调用需要一个操作模式。因此,要么将{action}
添加到 url 模式,要么将action=
设置为参数。这些操作必须与 @action 装饰器中定义的名称匹配。在您的示例中,您将操作命名为new_account
,但您使用new
操作调用了add_handler
。因此它们没有正确连接。Well a call to
add_handler
needs an action pattern. So that's either adding{action}
to the url pattern, or settingaction=
as an argument. Those actions must match the names defined in@action
decorators. In your example, you named the actionnew_account
, yet you calledadd_handler
with an action ofnew
. Thus they aren't properly connected.