Pylons Routes url_for for a map.resource
How do I get the get, post, put and delete URLs for a restful routes re开发者_运维百科source using url_for
?
For example, how do I get the PUT URL for a resource with id=1, and routes defined in routing.py
like so:
map.resource('user', 'users', controller='user')
I know the correct URL is /users/1
, but I don't want to hard code it.
Check out: http://routes.groovie.org/restful.html
url('user', id=1)
should give you '/users/1'
In routes.py your route should be:
map.resource('user', 'users/{id}', controller='user' action="some_action")
and in your controller you can get this URL with url_for
like this:
url_for(controller="user", action="some_action", id=1)
Ref: Chapter 9: URLs, Routing and Dispatch, Pylons book.
I must warn you that this was used in Pylons 0.9.7, but it is not used in Pylons 1.0. url_for
and redirect_to
are redesigned. If you want to redirect in your controller you must write:
redirect(url(controller="user", action="some_action", id=1))
Or in your case:
url(controller="user", action="some_action", id=1)
Ref.: Pylons 1.0 Released
精彩评论