Search code examples
pythonpyramidrestful-architectureakhet

pyramid_handlers not working


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.


Solution

  • Well a call to add_handler needs an action pattern. So that's either adding {action} to the url pattern, or setting action= as an argument. Those actions must match the names defined in @action decorators. In your example, you named the action new_account, yet you called add_handler with an action of new. Thus they aren't properly connected.