Working in Django 1.3, I had some trouble with my view code.
Actually, the error was pretty vague, something about my
urls.py containing no patterns. After much wailing and gnashing of teeth (during which I learned I really don’t know how to best use the debugger in Eclipse), I traced the problem down to three of my view classes. All three included a setting for the success_url attribute:
success_url = reverse('support_list')
Apparently, you can’t do that.
Once I found the offending line of code, something in my mind clicked on to remind me that I had hit this problem before. To solve it, I had moved the reverse statement into the
def get_success_url() return reverse('support_list')
Looking for a better solution, a quick Google Search brought up this post in in the django-users group.
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?
So others have hit the same problem. Looking further down the page, a more concise solution was offered:
success_url = lazy(reverse, str)("support_list")
I used this for my current project – a little cleaner.
In v1.4, we can use the ‘reverse_lazy()’ function in place of ‘reverse()’.
Suddenly, I understand the reason for the
SuccessURLRedirectListMixin in the popular Django Braces package. Using this mixin, one can declare the success url with a simple variable assignment (no lazy code required), and the mixin provides the
get_success_url() function needed to make everything work.
BTW, I highly recommend using Braces with class based views. I have found several to be quite useful to speed up coding and to keep things clean and clear.