route_url() to generate a URL with …/#/… (for Javascript)

28 views
Skip to first unread message

jens.t...@gmail.com

unread,
Mar 6, 2018, 4:04:25 PM3/6/18
to pylons-discuss
Hi,

Using request.route_url() I’m trying to generate a URL of the form: _app_url/#/foo?arg=bla

Using a route="/#/foo" escaped the hash, and so did "/{hash_}/foo", and that's according to the change comment for Pyramid v1.9. Furthermore, an _anchor is always placed after query string arguments and must not be empty, so using _anchor="" is also not an option.

The best I can come up with is assembling such a URL like so:

_app_url + "/#/foo?" + pyramid.url.urlencode({"arg": "bla"})

but that looks rather ugly.

What is the recommended way of generating such a URL string? (Leaving aside the sense or nonsense of such a URL…)

Thanks!
Jens


Michael Merickel

unread,
Mar 6, 2018, 9:28:39 PM3/6/18
to Pylons
Pyramid only generates urls in the format <scheme>://<host>:<port>/<script_name>/<path_info>?<query>#<anchor>. Your proposed scheme is attempting to marshal all of the data through the anchor. The general idea of such a url that puts the query string in the anchor is to hide that data from the server (anchors are not sent along with requests, they are only used by the user agent).

You probably want to define a function that builds an anchor and then you can pass it in as the _anchor value to request.route_url. The only trick there is to know that pyramid will url-quote that value, so you'll want to pass in an unquoted value.

def make_anchor(path, args):
    return ...

request.route_url(..., _anchor=make_anchor(...))

If you're feeling fancy you can even do this with a pregenerator on the route that you want this to work for which could take kwargs passed to route_url and convert them into an _anchor argument under the hood. This way you could do things like request.route_url(..., arg1='foo', arg2='bar') and the pregenerator could strip arg1/arg2 and convert them to an _anchor kwarg.

- Michael


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/09785b15-32f1-4f9a-98f5-67b77caa0080%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages