Django - is not a registered namespace
PythonDjangoPython Problem Overview
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.
Python Solutions
Solution 1 - Python
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'))
Solution 2 - Python
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')
]
Solution 3 - Python
I also faced the same issue. it is fixed now by adding
app_name = "<name of your app>"
in app/urls.py
Solution 4 - Python
For Django 3.0, if you're handling your urls within the app and using include
with path
, in your project/urls.py
:
urlpatterns = [
path(os.getenv('ADMIN_PATH'), admin.site.urls),
path('', include('my_simple_blog.urls', namespace='my_simple_blog')),
path('account/', include('account.urls', namespace='account')),
]
You need to specify namespace
in include
.
And then in your app/urls.py
:
app_name = 'account'
urlpatterns = [
path('register/', registration_view, name='register'),
path('logout/', logout_view, name='logout'),
path('login/', login_view, name='login'),
]
The app_name
must match the namespace
you've specified in project/urls.py
.
Whenever you're referring to these urls, you need to do it like this:
{% url 'namespace:name' %}
If you're using it with redirect
:
return redirect('namespace:name')
Solution 5 - Python
For the namespace error, Make sure you have linked the app's url in the main urls.py file
path('app_name/',include('app_name.urls'))
also in the urls.py of your app,make sure you mention the app's name as
app_name='app_name'
Also make sure you have registered the app's name on your installed apps in settings.py
Solution 6 - Python
As azmirfakkri has said if you're using redirect
, dont use this {% url 'namespace:name' %}
syntax, use return redirect('namespace:name')
.
Solution 7 - Python
Probably 2 things could be a root cause, in app/urls.py do include as below
app_name = 'required_name'
and in project urls.py also include the app_name
url(r'^required_name/$',views.home,name='required_name'),
Check: register app in settings.py INSTALLED_APPS
Solution 8 - Python
tag name must be unique in the urls.py
file inside your application package inside the project! it is important for the template tagging to route whats what and where.
now [1] inside the urls.py
file you need to declare the variable appName
and give it the unique value. for example appName = "myApp";
in your case myHomeApp and [2] also define the urlpatterns list...
urlpatterns = [..., url(r'^submit/$', views.submit, name='submit'), ...];
in the html file just change the url tag to:
<form action="{% url 'myHomeApp:submit' %}" method='post'>
this should sifuce... else just write here and we'll see how to continue on
Solution 9 - Python
A common mistake that I always find is when you have some name space in your template, and in YourApp.url you don't have any name space so if you should use name space add in YourApp.url something like this app_name = "blog"
then on your temples make sure you add your name space,
so you will have some thing like this "assumption errors are coming from edit.html
"
then on that particular template you will do this
"{% url 'blog:vote' pk=post.pk %}"
"{% url 'blog:post_category' category.name %}"
Solution 10 - Python
if you happen to be nesting include(
s, the namespace compounds, eg. topappname:appname:viewname
Solution 11 - Python
Maybe someone will find this suggestion helpful.
Go to your applications urls.py
and type this before the urlpatterns:
app_name = 'Your app name'
Solution 12 - Python
For anyone who struggled on this error like me: After reading the solutions to this question, I was setting namespace
in include
function in a wrong urls file. Make sure you are modifying the right urls file. For me it was putting it in the main url.py
besides settings.py
. I hope this answer helps anyone who got confused as I did.
Solution 13 - Python
I got the same error below:
> NoReverseMatch at /account/register/ 'account' is not a registered > namespace
So, I set "app_name" with the application name "account" to "account/urls.py" as shown below then the error above is solved:
# "account/urls.py"
from django.urls import path
from . import views
app_name = "account" # Here
urlpatterns = [
path("register/", views.register_request, name="register")
]