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 ?
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 .
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.
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.
In Django 1.3 and newer you can use resolve function:
from django.core.urlresolvers import resolve
print resolve('/books/').url_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With