75

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Programmerr
  • 961
  • 3
  • 9
  • 11
  • have you registered `home` as a namespace? – Sayse Jan 26 '17 at 21:26
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Jan 26 '17 at 21:26
  • to register is as a namespace isn't that just by putting 'home.apps.HomeConfig', in INSTALLED_APPS? – Programmerr Jan 26 '17 at 21:30
  • It has nothing to do with installed apps – Sayse Jan 26 '17 at 21:54
  • 1
    This is covered in the [official Django tutorial](https://docs.djangoproject.com/en/3.0/intro/tutorial03/#namespacing-url-names), and, in more detail, in the [docs](https://docs.djangoproject.com/en/3.0/topics/http/urls/#url-namespaces-and-included-urlconfs). – djvg Apr 13 '20 at 19:57

16 Answers16

96

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'))
mislavcimpersak
  • 2,880
  • 1
  • 27
  • 30
  • 5
    django 3 (and probably django 2): path('someurl', include(('home.urls', 'home'), namespace='home')) – mirek Feb 28 '20 at 19:34
  • I don't think you have updated the answer. It is not about url/path, but the 1st parameter in include() must be a tuple. Otherwise you will receive an error: "Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead." – mirek Mar 23 '20 at 18:05
  • in django 4.1, path('publisher-polls/', include('polls.urls', namespace='publisher-polls')), – MD Sulaiman Oct 10 '22 at 12:16
  • 1
    You should also define an app_name variable and set it to the name of your app (as a string) in your app.urls file. Like so, app_name='myapp'. If not, you will run into this error "django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported". (Django 4.1) – ismail pervez Feb 21 '23 at 07:15
33

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')
]
JamCon
  • 2,313
  • 2
  • 25
  • 34
vikasvmads
  • 542
  • 1
  • 7
  • 12
  • I want confirm that this works in django 3.0. Then {% url 'userinfo:...' %} can be used. And maybe only this way is possible. Following doesn't work for me: 1) name= or namespace= in project level urls.py, 2) settings in apps.py (maybe I don't know the correct one) – mirek Feb 28 '20 at 19:22
  • 1
    I need to correct my previous comment: This syntax in main urls.py works too: path('someurl', include(('home.urls', 'home'), namespace='home')) – mirek Feb 28 '20 at 19:36
  • This is outdated syntax. – Soerendip Apr 08 '21 at 17:35
25

I also faced the same issue. it is fixed now by adding

app_name = "<name of your app>" 

in app/urls.py

djvg
  • 11,722
  • 5
  • 72
  • 103
9

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')
azmirfakkri
  • 571
  • 1
  • 6
  • 18
9

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

  • I followed your steps and it was my problem. Here is exactly what I changed. I share it for those who wanted flesh in blood examples: https://github.com/kasir-barati/go-back-to-score-one-in-learning-django/commit/87a38ea7705d62d147f7485549eb029f45f925c6 – Kasir Barati Jul 03 '22 at 08:51
3

As azmirfakkri has said if you're using redirect, dont use this {% url 'namespace:name' %} syntax, use return redirect('namespace:name').

Yash Verma
  • 461
  • 5
  • 4
3

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

Ahmed Contrib
  • 646
  • 4
  • 5
2

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

mnille
  • 1,328
  • 4
  • 16
  • 20
Eyal Israel
  • 257
  • 4
  • 9
2

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 %}"

user3719458
  • 346
  • 3
  • 12
2

if you happen to be nesting include(s, the namespace compounds, eg. topappname:appname:viewname

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
1

Maybe someone will find this suggestion helpful.

Go to your applications urls.py and type this before the urlpatterns:

app_name = 'Your app name'
1

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 was confused as I was.

Salek
  • 449
  • 1
  • 10
  • 19
1

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")
]
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

Check your urls.py

urlpatterns = [
    re_path(r'^submit/expense/$', views.submit_expense, name='submit_expense'),
    re_path(r'^submit/income/$', views.submit_income, name='submit_income'),
    re_path(r'^register/$', views.register, name='register'),

]

then open template.html put for example register register in your HTML tag like this:

<a class="navbar-brand" href="{% url 'register' %}">
0

This worked for me:

In urls.py

urlpatterns = [
path("admin/", admin.site.urls),
path("", views.index),
path("test/", views.test, name = 'test')]

In views.py:

def test(request): 
return render(request, "base/test.html")

In the template:

 href="{% url 'test' %}"
0

I fixed it by using this on my main app's urls so when I use this way This error disappear

In the main folder, where the setting is there, go to urls and path("" , include("YourAppName.urls")),

Thornily
  • 533
  • 3
  • 15