Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 559
Alejandro Veintimilla Avatar asked Oct 16 '25 03:10

Alejandro Veintimilla


1 Answers

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.

like image 190
danielcorreia Avatar answered Oct 18 '25 20:10

danielcorreia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!