Getting the name of a matched URL pattern

9 views
Skip to first unread message

Nick Fishman

unread,
Jul 1, 2009, 9:56:59 PM7/1/09
to django...@googlegroups.com
Hey everyone,

I'm working with Django's named URL patterns, and was wondering how to
fetch the name of the URL pattern that triggered the view. For
example, with the following urlpatterns

urlpatterns = patterns('',
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True},
"arch-summary"),
)

is there any way to fetch the name="full-archive" and
name="arch-summary" parameter from inside the view?

I found a thread that talks about this same issue
(http://stackoverflow.com/questions/1042211/get-name-for-matched-url-pattern),
but the solution requires duplicating the URL pattern name in a
dictionary.

Thanks,

Nick

greatlemer

unread,
Jul 2, 2009, 2:03:32 AM7/2/09
to Django users
On Jul 1, 9:56 pm, Nick Fishman <bsdlogi...@bsdlogical.com> wrote:
> Hey everyone,
>
> I'm working with Django's named URL patterns, and was wondering how to
> fetch the name of the URL pattern that triggered the view. For
> example, with the following urlpatterns
>
> urlpatterns = patterns('',
>     url(r'^archive/(\d{4})/$', archive, name="full-archive"),
>     url(r'^archive-summary/(\d{4})/$', archive, {'summary': True},
> "arch-summary"),
> )
>
> is there any way to fetch the name="full-archive" and
> name="arch-summary" parameter from inside the view?
>
> I found a thread that talks about this same issue
> (http://stackoverflow.com/questions/1042211/get-name-for-matched-url-p...),
> but the solution requires duplicating the URL pattern name in a
> dictionary.
>
> Thanks,
>
> Nick

Hi,

I came across a similar problem when trying to put together my app for
handling menus (since I wanted to be able to specify menu items by url
names). Anyway the following was the method I finally came up with
which seems to work just fine for me -
http://code.google.com/p/greatlemers-django-tools/source/browse/trunk/gdt_nav/models.py#158

Hope this helps.

--
G

Shawn Milochik

unread,
Jul 2, 2009, 12:11:00 PM7/2/09
to django...@googlegroups.com


Can you get what you need within the view by using
request.build_absolute_uri()?

Nick Fishman

unread,
Jul 2, 2009, 9:17:34 PM7/2/09
to django...@googlegroups.com
>
> Hi,
>
> I came across a similar problem when trying to put together my app for
> handling menus (since I wanted to be able to specify menu items by url
> names).  Anyway the following was the method I finally came up with
> which seems to work just fine for me -
> http://code.google.com/p/greatlemers-django-tools/source/browse/trunk/gdt_nav/models.py#158
>
> Hope this helps.
>
> --
> G

That looks like it does exactly what I'm looking for. I wish there
were a way to find the URL name without having to call the resolver
again, but your method looks pretty fast. Thanks!

Shawn, request.build_absolute_uri() returns a full path like
http://hostname/path/goes/here/, but it doesn't tell me the name of
the URL pattern that caused the request to be resolved to a particular
view.

Nick

Reply all
Reply to author
Forward
0 new messages