How To set up a route with route variables appearing in any order in query string.

1 view
Skip to first unread message

afrotypa

unread,
Jul 6, 2009, 10:24:55 AM7/6/09
to pylons-discuss
how do you set up routes to match a url structure with query strings
where the route variables can appear in any position in the query
string?

i.e. which route will match all 3 of these urls and cause the
controller/action to be called with var1=1, var2=2, var3=3?

/controller/action/?var1=1&var2=2&var3=3
/controller/action/?var2=2&var1=1&var3=3
/controller/action/?var3=3&var2=2&var1=1

afrotypa

unread,
Jul 7, 2009, 7:08:26 AM7/7/09
to pylons-discuss
Will I need some advanced routes feature such as a 'function
condition' to make this happen? Or is there some more obvious (and/or
simplified) way (i.e regular expressions?) to accomplish this, which I
should be looking at?.

Piotr Kęplicz

unread,
Jul 7, 2009, 7:53:00 AM7/7/09
to pylons-...@googlegroups.com
afrotypa, wtorek 07 lipca 2009 13:08:

> Will I need some advanced routes feature such as a 'function
> condition' to make this happen? Or is there some more obvious (and/or
> simplified) way (i.e regular expressions?) to accomplish this, which I
> should be looking at?.

You could access these parameters with default routes using request.GET.

.pk.

afrotypa

unread,
Jul 7, 2009, 9:39:01 AM7/7/09
to pylons-discuss
You mean simply use

map.connect('/{controller}/{action}')

and then call request.params in the action to extract the query
variables directly from the request?

Perhaps that will work...

Didip Kerabat

unread,
Jul 7, 2009, 11:26:44 AM7/7/09
to pylons-...@googlegroups.com
There isn't a built in feature for that in Routes.

But you can use wildcard (*) and then split the wildcard using '/'.

By doing this, the "wildcard params" are stored in array, so that you can sort them however you like to fulfill your requirements.

- Didip -

Piotr Kęplicz

unread,
Jul 7, 2009, 1:01:45 PM7/7/09
to pylons-...@googlegroups.com
afrotypa, wtorek 07 lipca 2009 15:39:

> You mean simply use
>
> map.connect('/{controller}/{action}')
>
> and then call request.params in the action to extract the query
> variables directly from the request?

Exactly, request.params is a multidict with all the QUERY_STRING parameters.

.pk.

afrotypa

unread,
Jul 7, 2009, 1:51:25 PM7/7/09
to pylons-discuss
Thanks!.

On Jul 7, 11:26 am, Didip Kerabat <did...@gmail.com> wrote:
> There isn't a built in feature for that in Routes.
>
> But you can use wildcard (*) and then split the wildcard using '/'.
>
> By doing this, the "wildcard params" are stored in array, so that you can
> sort them however you like to fulfill your requirements.
>
> - Didip -
>

Mike Orr

unread,
Jul 7, 2009, 4:19:30 PM7/7/09
to pylons-...@googlegroups.com

Routes does not look at the query string, only the URL path. There is
no way to pass query parameters as action arguments.

Well, you could theoretically set a callback function
"map.connect(..., condition={'function': MY_FUNC})", and it could
parse the query string and stuff the variables into the match dict.
But I don't know if webob is set up at that point so you'd have to do
it manually.

--
Mike Orr <slugg...@gmail.com>

afrotypa

unread,
Jul 8, 2009, 9:50:31 AM7/8/09
to pylons-discuss
Great!. Thanks.

On Jul 7, 4:19 pm, Mike Orr <sluggos...@gmail.com> wrote:
> Mike Orr <sluggos...@gmail.com>
Reply all
Reply to author
Forward
0 new messages