librelist archives

« back to archive

Inverse of url_for?

Inverse of url_for?

From:
Michael Parker
Date:
2013-01-14 @ 03:14
Hi all,

Instead of url_for taking an endpoint and kwargs and producing a URL, is
there a method that takes a URL and returns its associated endpoint? I
assume this exists in order to route incoming requests, but is it
accessible in any way through the API?

Thanks,
Mike

Re: [flask] Inverse of url_for?

From:
Steven Kryskalla
Date:
2013-01-14 @ 04:17
On Sun, Jan 13, 2013 at 7:14 PM, Michael Parker
<michael.g.parker@gmail.com> wrote:
> Hi all,
>
> Instead of url_for taking an endpoint and kwargs and producing a URL, is
> there a method that takes a URL and returns its associated endpoint? I
> assume this exists in order to route incoming requests, but is it accessible
> in any way through the API?

Yes, you can use the "match" method on werkzeug's MapAdapter object,
which flask uses internally:

http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.MapAdapter.match

For example:

>>> app.url_map.bind('localhost')
<werkzeug.routing.MapAdapter object at 0xffa0238c>
>>> app.url_map.bind('localhost').match('/admin/')
('admin.admin_area', {})
>>> app.url_map.bind('localhost').match('/index')
('site.display_page', {'path': u'index'})

It looks like you can do this inside of a request like this:

from flask import _request_ctx_stack

...inside request...
ctx = _request_ctx_stack.top
ctx.url_adapter.match('/asdf')

-steve

Re: [flask] Inverse of url_for?

From:
Jose Ayerdis
Date:
2013-01-14 @ 07:17
What about using endpoint property from the request?

*https://github.com/mitsuhiko/flask/blob/master/flask/wrappers.py#L63*

Sincerly yours,

[Jose Luis Ayerdis Espinoza]
Necronet.info |
LinkedIn<http://www.linkedin.com/pub/jose-luis-ayerdis-espinoza/28/7b4/704>|
Careers
StackOverflow <http://careers.stackoverflow.com/necronet>


2013/1/14 Steven Kryskalla <skryskalla@gmail.com>

> On Sun, Jan 13, 2013 at 7:14 PM, Michael Parker
> <michael.g.parker@gmail.com> wrote:
> > Hi all,
> >
> > Instead of url_for taking an endpoint and kwargs and producing a URL, is
> > there a method that takes a URL and returns its associated endpoint? I
> > assume this exists in order to route incoming requests, but is it
> accessible
> > in any way through the API?
>
> Yes, you can use the "match" method on werkzeug's MapAdapter object,
> which flask uses internally:
>
> http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.MapAdapter.match
>
> For example:
>
> >>> app.url_map.bind('localhost')
> <werkzeug.routing.MapAdapter object at 0xffa0238c>
> >>> app.url_map.bind('localhost').match('/admin/')
> ('admin.admin_area', {})
> >>> app.url_map.bind('localhost').match('/index')
> ('site.display_page', {'path': u'index'})
>
> It looks like you can do this inside of a request like this:
>
> from flask import _request_ctx_stack
>
> ...inside request...
> ctx = _request_ctx_stack.top
> ctx.url_adapter.match('/asdf')
>
> -steve
>

Re: [flask] Inverse of url_for?

From:
Michael Parker
Date:
2013-01-16 @ 23:37
Thanks for the prompt reply! I'm just using this to ensure that a redirect
URL passed in as a query parameter to an endpoint is valid. (Mostly a
sanity check.)

- Mike


On Sun, Jan 13, 2013 at 11:17 PM, Jose Ayerdis <joseayerdis@gmail.com>wrote:

> What about using endpoint property from the request?
>
> *https://github.com/mitsuhiko/flask/blob/master/flask/wrappers.py#L63*
>
> Sincerly yours,
>
> [Jose Luis Ayerdis Espinoza]
> Necronet.info | 
LinkedIn<http://www.linkedin.com/pub/jose-luis-ayerdis-espinoza/28/7b4/704>|
Careers
> StackOverflow <http://careers.stackoverflow.com/necronet>
>
>
> 2013/1/14 Steven Kryskalla <skryskalla@gmail.com>
>
>> On Sun, Jan 13, 2013 at 7:14 PM, Michael Parker
>> <michael.g.parker@gmail.com> wrote:
>> > Hi all,
>> >
>> > Instead of url_for taking an endpoint and kwargs and producing a URL, is
>> > there a method that takes a URL and returns its associated endpoint? I
>> > assume this exists in order to route incoming requests, but is it
>> accessible
>> > in any way through the API?
>>
>> Yes, you can use the "match" method on werkzeug's MapAdapter object,
>> which flask uses internally:
>>
>> http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.MapAdapter.match
>>
>> For example:
>>
>> >>> app.url_map.bind('localhost')
>> <werkzeug.routing.MapAdapter object at 0xffa0238c>
>> >>> app.url_map.bind('localhost').match('/admin/')
>> ('admin.admin_area', {})
>> >>> app.url_map.bind('localhost').match('/index')
>> ('site.display_page', {'path': u'index'})
>>
>> It looks like you can do this inside of a request like this:
>>
>> from flask import _request_ctx_stack
>>
>> ...inside request...
>> ctx = _request_ctx_stack.top
>> ctx.url_adapter.match('/asdf')
>>
>> -steve
>>
>
>