pythonpython-3.xdjangodjango-viewsdjango-urls

Django - is not a registered namespace


I am trying to process a form in django/python using the following code.


home.html:

<form action="{% url 'home:submit' %}" method='post'>

views.py:

def submit(request):
    a = request.POST(['initial'])
    return render(request, 'home/home.html', {
        'error_message': "returned"
    })

urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^submit/$', views.submit, name='submit')
]

when I try to run it in a browser I get the error:

NoReverseMatch at /home/ u'home' is not a registered namespace

and another error message indicating a problem with the form.


Solution

  • You should just change you action url in your template:

    <form action="{% url 'submit' %} "method='post'>
    

    On the note of url namespaces...

    In order to be able to call urls using home namespace you should have in your main urls.py file line something like:

    for django 1.x:

    url(r'^', include('home.urls', namespace='home')),
    

    for django 2.x and 3.x

    path('', include(('home.urls', 'home'), namespace='home'))