Django 'resolve' : get the url name instead of the view_function

sebpiq picture sebpiq · Jul 2, 2010 · Viewed 9.1k times · Source

My problem is simple, I have an url, I would like to resolve it, but get the url name instead of the view function associated with it ...

For example... this is urlconf :

urlpatterns = patterns('',
...
    url('^/books/$', book_list, name="overview_books"),
...
)

And this is what I would like :

>>> resolve('/books/')
'overview_books'

Do you know any way to do this ?

Answer

barszczmm picture barszczmm · Jun 1, 2012

In Django 1.3 and newer you can use resolve function:

from django.core.urlresolvers import resolve
print resolve('/books/').url_name