基于 Django Class 的 success_url 的反向视图抱怨循环导入
reverse for success_url on Django Class Based View complain about circular import
当使用基于方法的视图时,使用 reverse
重定向并没有抱怨这个并且仍然可以找到根 url conf。但是,在基于 class 的视图中,它抱怨:
ImproperlyConfigured at /blog/new-post/
The included urlconf 'blog.urls' does not appear to have any
patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
我的class是这样定义的:
class BlogCreateView(generic.CreateView):
form_class = Blog
template_name = 'blog/new-post.html'
success_url = reverse('blog:list-post')
如何在基于 class 的视图中为 success_url
正确使用 reverse
?谢谢。
PS: 我对为什么在出现此错误后需要重新启动 runserver
很感兴趣(不像 [=18= 这样的错误) ]不需要重启runserver
)
在您的方法中使用 reverse
是可行的,因为 reverse
在视图为 运行 时被调用。
def my_view(request):
url = reverse('blog:list-post')
...
如果你覆盖get_success_url
,那么你仍然可以使用reverse
,因为get_success_url
在视图为运行时调用reverse
。
class BlogCreateView(generic.CreateView):
...
def get_success_url(self):
return reverse('blog:list-post')
但是,您不能将 reverse
与 success_url
一起使用,因为这样 reverse
在导入模块时会在加载 url 之前被调用。
覆盖 get_success_url
是一种选择,但最简单的解决方法是使用 reverse_lazy
而不是反向。
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')
要回答关于重新启动 运行 服务器的最后一个问题,ImproperlyConfigured
错误与 TemplateDoesNotExists
不同,因为它发生在加载 Django 应用程序时。
尝试在您的 CBV 中使用 reverse_lazy
instead of reverse
。它是 reverse
的延迟评估版本。它不会执行,直到需要该值。
from django.core.urlresolvers import reverse_lazy
class BlogCreateView(generic.CreateView):
form_class = Blog
template_name = 'blog/new-post.html'
success_url = reverse_lazy('blog:list-post')
当使用基于方法的视图时,使用 reverse
重定向并没有抱怨这个并且仍然可以找到根 url conf。但是,在基于 class 的视图中,它抱怨:
ImproperlyConfigured at /blog/new-post/
The included urlconf 'blog.urls' does not appear to have any
patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
我的class是这样定义的:
class BlogCreateView(generic.CreateView):
form_class = Blog
template_name = 'blog/new-post.html'
success_url = reverse('blog:list-post')
如何在基于 class 的视图中为 success_url
正确使用 reverse
?谢谢。
PS: 我对为什么在出现此错误后需要重新启动 runserver
很感兴趣(不像 [=18= 这样的错误) ]不需要重启runserver
)
在您的方法中使用 reverse
是可行的,因为 reverse
在视图为 运行 时被调用。
def my_view(request):
url = reverse('blog:list-post')
...
如果你覆盖get_success_url
,那么你仍然可以使用reverse
,因为get_success_url
在视图为运行时调用reverse
。
class BlogCreateView(generic.CreateView):
...
def get_success_url(self):
return reverse('blog:list-post')
但是,您不能将 reverse
与 success_url
一起使用,因为这样 reverse
在导入模块时会在加载 url 之前被调用。
覆盖 get_success_url
是一种选择,但最简单的解决方法是使用 reverse_lazy
而不是反向。
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')
要回答关于重新启动 运行 服务器的最后一个问题,ImproperlyConfigured
错误与 TemplateDoesNotExists
不同,因为它发生在加载 Django 应用程序时。
尝试在您的 CBV 中使用 reverse_lazy
instead of reverse
。它是 reverse
的延迟评估版本。它不会执行,直到需要该值。
from django.core.urlresolvers import reverse_lazy
class BlogCreateView(generic.CreateView):
form_class = Blog
template_name = 'blog/new-post.html'
success_url = reverse_lazy('blog:list-post')