Generate route url with route pattern intact

44 views
Skip to first unread message

Mattias

unread,
Feb 17, 2012, 4:25:55 AM2/17/12
to pylons-discuss
Hello.

If I define a route like "config.add_route('user, '/user/{id}/
{action}')" is there anyway to generate the url 'http://server.com/
user/{id}/{action}'? Right now I do "request.route_url('user',
id='{id}', action='{action}')" which works but means I have to
remember the exact pattern parameters.

The reason I am asking is that we want our api for our webapp to use
the REST url style so we have to try to avoid sticking the id in the
query, which means that we won't be able to use the regular jquery
ajax call like
"$.post('http://server.com/user',
{'id': 1, 'action': 'send-email'},
function(response){alert(response)}
)"

Instead we will use https://github.com/lyconic/jquery.rest
"$.read('http://server.com/user/{id}/{action}',
{'id': 1, 'action': 'send-email'},
function(response){alert(response)}
)"

But due to this we have to get the urls in their "raw" form.

Robert Forkel

unread,
Feb 17, 2012, 4:41:55 AM2/17/12
to pylons-...@googlegroups.com
hi mattias,
i pass all route names and patterns to my javascript code with a mako
template looking like this:

<script>
% for route in request.registry.getUtility(IRoutesMapper).get_routes():
APP.config.routes["${route.name}"] = "${route.path}";
% endfor
</script>

so that i can create URLs in javascript with a function like

APP.route = function (name, args) {
var path = APP.config.routes[name];

for (key in args) {
if (args.hasOwnProperty(key) && args[key] !== undefined) {
path = path.replace('{' + key + '}', args[key]);
}
}

return path;
};

The above only works if the match pattern does only contain names in
curly braces, but could be extended to massage more complex patterns
into this simple form ({id:\d+} -> {id}, ...).

> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>

Mattias

unread,
Feb 17, 2012, 5:00:50 AM2/17/12
to pylons-discuss
Thank you. That seems exactly like what I want to do.

Is "request.registry.getUtility(IRoutesMapper).get_routes()" an
official Pyramid api or is it just a implementation detail that may
change without prior notice?
> > Instead we will usehttps://github.com/lyconic/jquery.rest

Robert Forkel

unread,
Feb 17, 2012, 5:09:03 AM2/17/12
to pylons-...@googlegroups.com
On Fri, Feb 17, 2012 at 11:00 AM, Mattias <mattias.g...@gmail.com> wrote:
> Is "request.registry.getUtility(IRoutesMapper).get_routes()" an
> official Pyramid api or is it just a implementation detail that may
> change without prior notice?

good question. maybe introspection [1] would work, too?

[1] http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/introspector.html

Michael Merickel

unread,
Feb 17, 2012, 7:57:43 AM2/17/12
to pylons-...@googlegroups.com
On Fri, Feb 17, 2012 at 4:00 AM, Mattias <mattias.g...@gmail.com> wrote:
Is "request.registry.getUtility(IRoutesMapper).get_routes()" an
official Pyramid api or is it just a implementation detail that may
change without prior notice?

The route mapper is not a public API. To do it you'd have to use the introspector as Robert suggested, but from there you can access the Route object which has the pattern.

Mattias

unread,
Feb 17, 2012, 8:28:58 AM2/17/12
to pylons-discuss
Ok. How stable is the latest 1.3 alpha? Are there any major plans for
api reorganisation before it hits beta? As long as the api is stable
we might as well use 1.3 in development.

On Feb 17, 1:57 pm, Michael Merickel <mmeri...@gmail.com> wrote:
> On Fri, Feb 17, 2012 at 4:00 AM, Mattias <mattias.gyllsdo...@gmail.com>wrote:
>
> > Is "request.registry.getUtility(IRoutesMapper).get_routes()" an
> > official Pyramid api or is it just a implementation detail that may
> > change without prior notice?
>
> The route mapper is not a public API. To do it you'd have to use the
> introspector as Robert suggested, but from there you can access the Route
> object which has the pattern.
>
> http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/inte...

Chris McDonough

unread,
Feb 17, 2012, 8:38:32 AM2/17/12
to pylons-...@googlegroups.com
On Fri, 2012-02-17 at 05:28 -0800, Mattias wrote:
> Ok. How stable is the latest 1.3 alpha? Are there any major plans for
> api reorganisation before it hits beta? As long as the api is stable
> we might as well use 1.3 in development.

No plans to change the API. The first beta is due soon, FWIW.

- C

Reply all
Reply to author
Forward
0 new messages