Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - is not a registered namespace

Tags:

python

django

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.

like image 916
Programmerr Avatar asked Jan 26 '17 21:01

Programmerr


People also ask

What is namespace in Django?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.

How do I reverse in Django?

reverse() If you need to use something similar to the url template tag in your code, Django provides the following function: reverse (viewname, urlconf=None, args=None, kwargs=None, current_app=None)


2 Answers

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')) 
like image 160
mislavcimpersak Avatar answered Sep 21 '22 12:09

mislavcimpersak


In your main project, open url.py first. Then check, there should be app_name declared at first. If it is not, declare it.

For example, my app name is user info which is declared in url.py

app_name = "userinfo"  urlpatterns = [     url(r'home/', views.home, name='home'),     url(r'register/', views.registration, name='register') ] 
like image 41
vikasvmads Avatar answered Sep 19 '22 12:09

vikasvmads