Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django reverse causing url circular import, why?

I get this error:

The included urlconf 'fouraxis.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.

I know the url pattern has something in it, it looks like this:

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")
    ]

The RegistroUsuario view looks like this:

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}

I understand I can replace the reverse with a plain-text url like this perfil/mi_perfil. But, I want to know why is this happening with reverse, I can't find the explanation on de docs. Also, using reverse is better cause it is dynamic (if anytime I change the url, it still works as long as it keeps its name).

like image 308
Alejandro Veintimilla Avatar asked Dec 01 '15 22:12

Alejandro Veintimilla


People also ask

What causes circular import in Django?

Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error.

What does the Django urls reverse () function do?

reverse_lazy()providing a reversed URL as the url attribute of a generic class-based view. providing a reversed URL to a decorator (such as the login_url argument for the django.


1 Answers

The reverse() call is made when the view is imported, which is probably when the urlconf is first loaded. You need to use reverse_lazy() instead:

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}
like image 116
knbk Avatar answered Nov 04 '22 09:11

knbk