Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

django reverse with namespace

I'm getting this error:

The included urlconf 'unsitiodigital.urls' does not appear to have any patterns in it.

The traceback points to this line

class Contacto(FormView):
    template_name = "contacto.html"
    form_class = FormContacto
    success_url = reverse("main:mensaje_enviado") -->This Line

    def form_valid(self, form):

        form.send_email()
        return super(Contacto, self).form_valid(form)

There are valid patterns, it works without the reverse line. urls.py - general

urlpatterns = [    
    url(r'^', include('main.urls', namespace="main")),
    url(r'^admin/', include(admin.site.urls)),
]

urls.py - main

from django.conf.urls import url

from main import views

urlpatterns = [
        url(r'^$', views.Inicio.as_view(), name='inicio'),
        url(r'^quienes_somos/$', views.QuienesSomos.as_view(), name='quienes_somos'),
        url(r'^opciones/$', views.Opciones.as_view(), name='opciones'),
        url(r'^contacto/$', views.Contacto.as_view(), name='contacto'),
->      url(r'^mensaje_enviado/$', views.MensajeEnviado.as_view(), name='mensaje_enviado')
]

So, ¿which is the correct user of reverse?. Thanks a lot.

Upvotes: 4

Views: 7938

Answers (1)

danielcorreia
danielcorreia

Reputation: 2136

The include path must be wrong

url(r'^', include('main.urls', namespace="main")),  # the 'main.urls' path must be wrong

There are some ways you can include other urls. Try to import the patterns from the main.url module instead

from main.urls import urlpatterns as main_urls

url(r'^', include(main_urls, namespace="main"),

You also have to use reverse_lazy in the success_url.

from django.core.urlresolvers import reverse_lazy

class Contacto(FormView):
    template_name = "contacto.html"
    form_class = FormContacto
    success_url = reverse_lazy("main:mensaje_enviado")

It is useful for when you need to use a URL reversal before your project’s URLConf is loaded.

Upvotes: 10

Related Questions