How do I specify a default parameter value for a Pylons action?
I have an action which looks like this:
class ArticleController(BaseController):
def all(self, page, pagesize):
I want to be able to access /article/all/{page}/{pagesize}
, with default values for page
and pagesize
.
I tried setting the default values in the action method, but then apparently both page
and pagesize
get set to the default value if I only set a page
value.
I also tried something like this, but it doesn't work either:
map.connect('/article/all/{page}/{pagesize}', controller='article',
action='all')
map.connect('/', controller='article', action='all', page=0, pagesize=5)
map.connect('/article/all/', controller='article', action='all', page=0,
pagesize=5)
Actually, in that case it works when I access /
or /article/all/
.
But it doesn't work with /article/all
(even when I remove the trailing /
in the route accordingly).
Looking at Routes' documentation it looks like default values shouldn't work at all in that case, so maybe it's some kind or undefined behavior开发者_开发技巧.
Anyway, my question is, how can I get all()
to be called with default values for page
and pagesize
when accessing /article/all
and /article/all/42
?
(I know I could use the query string instead. map.redirect()
also kind of does the trick, but I don't really want to redirect.)
Your routes should look like that:
map.connect('/article/all',
controller='Article', action='all',
page=0, pagesize=5)
map.connect('/article/all/{page}',
controller='Article', action='all',
pagesize=5)
map.connect('/article/all/{page}/{pagesize}',
controller='Article', action='all')
You don't have to put default values in the method itself. So your controller should look like this:
class ArticleController(BaseController):
def all(self, page, pagesize):
return 'Page: %s. Pagesize: %s.' % (page, pagesize)
精彩评论