Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 404
sebpiq Avatar asked Jul 02 '10 10:07

sebpiq


People also ask

How do I name a URL in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .

How do I pass URL parameters in Django?

Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.

What does the Django URLs reverse () function do?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. The redirect variable is the variable here which will have the reversed value. So the reversed url value will be placed here.


1 Answers

In Django 1.3 and newer you can use resolve function:

from django.core.urlresolvers import resolve
print resolve('/books/').url_name
like image 189
barszczmm Avatar answered Oct 21 '22 02:10

barszczmm