How to get a reverse URL for a generic view? How to get a reverse URL for a generic view? django django

How to get a reverse URL for a generic view?


The only way to use reverse with generic views - named urls config.

urlpatterns += patterns('django.views.generic.list_detail',  (r'^posts/(?P<slug>[-\w]+)/$', 'object_detail',                          object_detail_info_dict, 'post_detail'),)reverse('post_detail', args=('foobar',))


This question seems to be for older versions of Django. I'm not familiar with how the old generic views work. But the new class-based generic views have the same problem.

Reversing doesn't work "out of the box" because View.as_view() returns a different wrapper function each time, and they don't compare equal to each other, so reverse() can't find the reverse route by comparing two functions that aren't equal.

There is another way, although it's non-standard. This is what I do for my class-based views:

class OrderView(LoginRequiredMixin, CreateView):    model = Order    form_class = OrderFormOrderView.plain_view = staticmethod(OrderView.as_view())

In this case, I use plain_view to mean the view returned by as_view() with no arguments. If you pass arguments to as_view(), then the wrapper it returns will actually be different to the plain one. So if you need both, you'd have to assign them to different properties:

OrderView.plain_view = staticmethod(OrderView.as_view())OrderView.bonk_view = staticmethod(OrderView.as_view(whee='bonk'))

You can link to these view attributes in urls.py:

urlpatterns = patterns('',    url(r'^order/$', views.OrderView.plain_view),    url(r'^frob/$', views.OrderView.bonk_view),

and then you can reverse them by reversing the view attributes:

def get_success_url(self):    return reverse(OrderView.plain_view)def get_failure_url(self):    return reverse(OrderView.bonk_view)