Using success_url with reverse() in class-based generic view

18,731 views
Skip to first unread message

jnns

unread,
Feb 18, 2011, 5:00:06 PM2/18/11
to Django users
Hi users,

I have a CreateView which I'd like to redirect to a custom success_url
defined in my URLconf. As I want to stick to the DRY-principle I just
did the following:

success_url = reverse("my-named-url")

Unfortunately, this breaks my site by raising an
"ImproperlyConfigured: The included urlconf doesn't have any patterns
in it". Removing success_url and setting the model's
get_absolute_url() to the following works fine:

def get_absolute_url(self):
return reverse("my-named-url")

I could reproduce this with a brand new project/application so I don't
think this has something to do with my setup.

Can anyone confirm this issue?

rb

unread,
Mar 22, 2011, 5:53:27 PM3/22/11
to Django users
I just ran into this today and was curious. My setup is this:

class MyGenericView(DeleteView):
success_url = reverse('my-name')

This will produce the from the original author. I got around this by
overriding get_success_url():

class MyGenericView(DeleteView):
def get_success_url(self):
return reverse('my-name')

Is this the preferred/proper way to accomplish this? While looking
around in the tickets I did find this ticket:

http://code.djangoproject.com/ticket/13753

This made me believe that maybe the generic views were using
redirect() (which can accept names), but I tried setting success_url
to 'my-name' and it sends an HttpRedirectResponse with that as the
target. Looking in django.views.generic.edit shows that it is just
calling HttpRedirectResponse. Should these call redirect() instead of
HttpRedirectResponse?

Thanks!

Rob

(django 1.3 rc 1 SVN-15894)

Rytis Sileika

unread,
Mar 28, 2011, 9:59:06 AM3/28/11
to Django users
Had the same issue. Here's what I did in my views.py:

---
from django.utils.functional import lazy

...

class MyDeleteView(DeleteView):
...
success_url = lazy(reverse, str)("success_url_name")
---

Again, I don't know if that's the correct way of doing things, but
worked for me. I think I saw a ticket somewhere that was planning to
implement the lazy_reverse functionality in code Django, but can't
find it, or may be I'm just making things up...

Daniel P

unread,
Sep 20, 2011, 6:08:37 PM9/20/11
to django...@googlegroups.com
Same problem. You'r not alone!

this is also a solution: http://djangosnippets.org/snippets/2445/

Xavier Ordoquy

unread,
Sep 21, 2011, 3:53:56 PM9/21/11
to django...@googlegroups.com
Hi,

You can also use get_success_url for that:

class ContactView(generic.FormView):
form_class = ContactForm

def get_success_url(self):
return reverse('contact-sent')

Regards,
Xavier

Linovia.

Le 21 sept. 2011 à 00:08, Daniel P a écrit :

> Same problem. You'r not alone!
>
> this is also a solution: http://djangosnippets.org/snippets/2445/
>

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Germán

unread,
Sep 26, 2012, 1:32:21 PM9/26/12
to django...@googlegroups.com
Just for the record.

Since Django 1.4, the best way to set up success_url in class-based generic views with url names is:
success_url = reverse_lazy('my_url_name')

Kurtis Mullins

unread,
Sep 26, 2012, 1:44:26 PM9/26/12
to django...@googlegroups.com
Sometimes, though, you may need to pass variables to a success url. In that case, I'd use the "get_success_url" method so you can access the 'self' attributes.

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/bm1tU2fvPAAJ.

Germán

unread,
Sep 26, 2012, 2:17:21 PM9/26/12
to django...@googlegroups.com
Indeed

Mayur Karmakar

unread,
Mar 11, 2018, 11:52:59 AM3/11/18
to Django users
Try to import reverse_lazy form "django.core.urlresolvers" in your views.py and declare the DeleteView as:-

class MyDeleteView(DeleteView):
      model = models.YourModelName
      success_url = reverse_lazy('Your_app_name_for_urls.py':urlpattern_name_your_wanna_redirect_to)
Reply all
Reply to author
Forward
0 new messages