django tutorial pt.5 namespace problem caused by client.get(reverse('polls:index'))

hi,
I am following tutorial and I get a bug that I can’t find solution to.
the problem is the same in shell and when writing a test in tests.py.

for this test:

class PollsTests(TestCase):
    def test_index_view(self):
        response = self.client.get(reverse('polls:index'))
        self.assertEqual(response.status_code, 200)

I get following error:

Traceback (most recent call last):
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\urls\base.py”, line 71, in reverse
extra, resolver = resolver.namespace_dict[ns]
~~~~~~~~~~~~~~~~~~~~~~~^^^^
KeyError: ‘namespace’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “B:\allCodingRelated\django\firstProject\mysite\polls\tests.py”, line 11, in test_index_view
response = self.client.get(reverse(‘polls:index’))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\client.py”, line 1049, in get
response = super().get(path, data=data, secure=secure, headers=headers, **extra)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\client.py”, line 465, in get
return self.generic(
^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\client.py”, line 617, in generic
return self.request(**r)
^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\client.py”, line 1013, in request
self.check_exception(response)
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\client.py”, line 743, in check_exception
raise exc_value
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\core\handlers\base.py”, line 220, in _get_response
response = response.render()
^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\response.py”, line 114, in render
self.content = self.rendered_content
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\response.py”, line 92, in rendered_content
return template.render(context, self._request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\backends\django.py”, line 61, in render
return self.template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\base.py”, line 171, in render
return self._render(context)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\test\utils.py”, line 111, in instrumented_test_render
return self.nodelist.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\base.py”, line 1000, in render
return SafeString(“”.join([node.render_annotated(context) for node in self]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\base.py”, line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\template\defaulttags.py”, line 479, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\urls\base.py”, line 82, in reverse
raise NoReverseMatch(“%s is not a registered namespace” % key)
django.urls.exceptions.NoReverseMatch: ‘namespace’ is not a registered namespace

but as far as I am concerned I have everything well set up

that in my global (mysite) urls.py:

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path("huj/", include("huj.urls")),
    path("polls/", include("polls.urls", namespace="polls")),
    path('admin/', admin.site.urls),
]

and thats my urls.py in polls directory:

from django.urls import path
from . import views

app_name = "polls"

urlpatterns = [
    path("", views.IndexView.as_view(), name="index"),
    path("<int:pk>/", views.DetailView.as_view(), name="detail"),
    path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

I have polls in installed_aps in settings.py and my templates are in polls/templates/polls

INSTALLED_APPS = [
    #adding polls to apps
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

also worth mentioning that:
reverse('polls:index')
correctly returns:
/polls/
so I assume it is a problem of client.get()
but I have no idea how to fix that and I have ran out of ideas what more can I do/check

Welcome @drycod !

<conjecture>
The tutorial does not specify that you should use the namespace parameter on your include directive for the polls urls.

I would be curious to see if that’s causing some kind of redundancy / conflict when used in combination with this:

As a quick check, I’d try removing it from the url just to see what happens.
</conjecture>

Note: This really is just a guess - I have no direct knowledge in this area, I just noticed the difference between what you have here and what the tutorial shows. Unfortunately, I’m not in a position to check this myself at the moment.

It is not but when I was searching solution i stumbled across info to define it like that. but no matter if I have ’ , namespace=“polls” ’ or don’t have it, it doesn’t change anything and in either case won’t work

What version of Django and Python are you using?

(venv) PS B:\allCodingRelated\django\firstProject\mysite> python --version
Python 3.12.4
(venv) PS B:\allCodingRelated\django\firstProject\mysite> python -m django --version
5.0.7

I am unable to recreate the results you are reporting with the information provided here so far.

Please post your complete tests.py file.

Also, I see where your root urls is referencing a huj.urls definition. Is there any potential conflict between the urls in that file and the one in your polls.urls?

I just deleted the huj app alltogether but it doesn’t change anything

here is the whole tests.py(without other tests as deliting them doesn’t fix it and let’s keep it relevant):

import datetime

from django.test import TestCase, client
from django.utils import timezone
from django.urls import reverse


from .models import Question

class PollsTests(TestCase):
    def test_index_view(self):
        response = self.client.get(reverse('polls:index'))
        self.assertEqual(response.status_code, 200)

There’s something else interfering here, because I’m still mirroring what you’re posting here, and I’m not getting any error. The tests are running exactly as expected.

That generally implies one of:

  • There’s something in another file causing a conflict. (Need to check all files, including views, models, and apps, possibly settings.)

  • You have a __pycache__ file that hasn’t been properly cleaned out. (It’s worth ensuring that all __pycache__ directories have been emptied or removed.)

  • Something has gotten your venv messed up. (Might be worth trying to create a new venv to check this.)

i thinck problem is in templates, not views.py or test.py

File “C:\Users\suchw\AppData\Roaming\JetBrains\PyCharm2024.1\light-edit\venv\Lib\site-packages\django\**template**\**defaulttags.py**”, line 479, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

it means that you called url named by “namespace” in template.

i think you write template like this:
so, find template used in IndexView.

{% url 'namesplace' %}