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