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


when using method-based view, redirecting reverse didn't complain , can still find root url conf. but, in class-based views, complain:

improperlyconfigured @ /blog/new-post/  included urlconf 'blog.urls' not appear have patterns in it. if see valid patterns in file issue caused circular import. 

my class defined this:

class blogcreateview(generic.createview):     form_class = blog     template_name = 'blog/new-post.html'     success_url = reverse('blog:list-post') 

how use reverse success_url in class-based views? thanks.

ps: , i'm interested in why it's need restart runserver after error (not error templatedoesnotexists no need restart runserver)

using reverse in method works because reverse called when view run.

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

if overrride get_success_url, can still use reverse, because get_success_url calls reverse when view run.

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

however, can't use reverse success_url, because reverse called when module imported, before urls have been loaded.

overriding get_success_url 1 option, easiest fix use reverse_lazy instead of reverse.

from django.core.urlresolvers import reverse_lazy  class blogcreateview(generic.createview):     ...     success_url = reverse_lazy('blog:list-post') 

to answer final question restarting runserver, improperlyconfigured error different templatedoesnotexists because occurs when django application loaded.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -