我收到此错误:
包含的urlconf'fourraxis.urls'似乎没有任何模式.如果您在文件中看到有效模式,则问题可能是由循环导入引起的.
我知道url模式中有一些内容,它看起来像这样:
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^perfil/', include('clientes.urls'), namespace="cliente"), url(r'^admin/', include(admin.site.urls)) ]
clientes.urls:
from django.conf.urls import url from django.contrib.auth import views as auth_views from clientes import views urlpatterns = [ # login url(r'^login/$', auth_views.login, {'template_name': 'perfiles/login.html'}, name="login"), url(r'^logout/$', auth_views.logout, {'template_name': 'perfiles/logged_out.html'}, name="login"), url(r'^mi_perfil/$', views.mi_perfil, name="mi_perfil"), url(r'^registro_usuario/$', views.RegistroUsuario.as_view(), name="registro_usuario") ]
RegistroUsuario视图如下所示:
class RegistroUsuario(FormView): template_name = "perfiles/registro_usuario.html" form_class = UserCreationForm success_url = reverse("cliente:mi_perfil") # THIS REVERSE def form_valid(self, form): return redirect("cliente:mi_perfil") context = {'form': UserCreationForm}
我知道我可以用这样reverse
的纯文本网址替换它perfil/mi_perfil
.但是,我想知道为什么这种情况发生逆转,我无法找到关于de docs的解释.另外,使用reverse是更好的,因为它是动态的(如果我改变url,只要它保持其名称,它仍然有效).
在reverse()
导入视图时进行调用,这可能是在首次加载urlconf时.您需要使用reverse_lazy()
:
from django.core.urlresolvers import reverse_lazy class RegistroUsuario(FormView): template_name = "perfiles/registro_usuario.html" form_class = UserCreationForm success_url = reverse_lazy("cliente:mi_perfil") # THIS REVERSE def form_valid(self, form): return redirect("cliente:mi_perfil") context = {'form': UserCreationForm}