Many Thanks. Your explanation and the help I got on the #pylons
channel have caused me to rethink routes.

I now see that my initial quick and dirty implementation is actually
far closer to correct than what I thought, and it just needs some
tweaking :)


On Jul 21, 7:00 pm, Mike Orr <[email protected]> wrote:
> On Tue, Jul 21, 2009 at 2:55 PM, The Devil's
>
>
>
> Programmer<[email protected]> wrote:
>
> > For the sake of simplicity, let's say my site is accessible through /
> > test/
>
> > Now what if I also want an optional page parameter like /test/page6 ?
>
> > For that, I set up this route:
>
> > map.connect('/test/page:page', controller='test', action='test',
> > page=1)
>
> > Sadly, this only works if I include the page parameter.
>
> > I have read through the routes documentation many times (which is
> > quite small) and tried a bunch of different configurations, but I
> > cannot seem to find a way to make the page parameter optional.
>
> > Somebody put me out of my misery, please :) This has already consumed
> > more time than I'd like to admit.
>
> For an optional component you'd define two routes.  (Or enable
> minimization but that is error-prone.)  I'm also not sure about
> matching a partial URL component rather than an entire component,
> although I guess it might be OK.
>
> map.connect("/test", controller="test", action="test", page=1)
> map.connect("/test/{page}", controller="test", action="test")
>
> This would match:
>
>     /test  ->  test(page=1)
>     /test/2  ->  test(page="2")
>
> It may be more REST-ly correct to do something like this:
>
> map.connect("/test/pages/{page}", controller="test", action="test")
>
> And then make /test redirect to /test/pages/1.
>
> map.redirect("/test", "/test/pages/1")
>
> You could also make page a query parameter, in which case it would be
> outside the routing system.
>
> map.connect("/test", controller="test", action="test")
>     /test?page=2
>     def test(self):
>         page = request.params.get("page", 1)
>         page = int(page)   # Raises ValueError
>
> --
> Mike Orr <[email protected]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to