tags:

views:

41

answers:

1

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.)

+2  A: 

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)
Antoine Leclair
It doesn't work: `TypeError: all() takes exactly 3 arguments (1 given)`.The [routes manual](http://routes.groovie.org/manual.html) says “(The extra variable is *not* used for matching unless minimization is enabled.)”But for some reason, it works if I use for example /article/all/ (with a trailing /, both in routes and in the address request).
Bastien Léonard
Apparently, having several URLs for the same resource is considered as a bad practice (http://pylonsbook.com/en/1.1/urls-routing-and-dispatch.html#route-minimization). So I guess I'll stick to `redirect()`.
Bastien Léonard
Weird it doesn't work for you. It works perfeclty here with a fresh new Pylons 1.0 projet. Also I guess you're right with the redirection.
Antoine Leclair