reverse for success_url on Django Class Based View complain about circular import

Using reverse in your method works because reverse is called when the view is run.

def my_view(request):
    url = reverse('blog:list-post')
    ...

If you overrride get_success_url, then you can still use reverse, because get_success_url calls reverse when the view is run.

class BlogCreateView(generic.CreateView):
    ...
    def get_success_url(self):
        return reverse('blog:list-post')

However, you can’t use reverse with success_url, because then reverse is called when the module is imported, before the urls have been loaded.

Overriding get_success_url is one option, but the easiest fix is to use reverse_lazy instead of reverse.

from django.urls import reverse_lazy
# from django.core.urlresolvers import reverse_lazy  # old import for Django < 1.10

class BlogCreateView(generic.CreateView):
    ...
    success_url = reverse_lazy('blog:list-post')

To answer your final question about restarting runserver, the ImproperlyConfigured error is different from TemplateDoesNotExists because it occurs when the Django application is loaded.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)