Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - using reverse() on Class-based views

I have the following urls configuration in my Django project:

urlpatterns = patterns('',
    (r'^my-view$', MyViewClass.as_view()),
)

Is there a way to use the reverse() function to get the url of the above view?

like image 498
Tzach Avatar asked Aug 05 '14 13:08

Tzach


1 Answers

Yes there is.

Use the name argument of the url function to define a name for the url, then you can use reverse on this name:

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^my-view$', MyViewClass.as_view(), name='my_view'),
)

reverse('my_view')
like image 106
Tzach Avatar answered Oct 30 '22 08:10

Tzach