Django polls app tutorial error

Hello forum, first post here. I am following the polls app tutorial, part 5. At the Django test client section, I am getting error when running the commande “response = client.get(reverse(‘polls:index’))” as shown below: (Sorry if this is not properly formatted

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> from django.test import Client
>>> client = Client()
>>> response = client.get('/')
Not Found: /
>>> response.status_code
404
>>> from django.urls import reverse
>>> response = client.get(reverse('polls:index'))
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.10/site-packages/django/urls/base.py", line 71, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'polls'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/homebrew/lib/python3.10/site-packages/django/urls/base.py", line 82, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'polls' is not a registered namespace

I have app_name defined in polls/urls.py. But frankly, not sure what it does as I don’t see it referenced anywhere

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'),
]

What about your main urls.py?
In there you should have an entry with the include function to the “polls.urls”, in there you can set the namespace to polls.
For example:

from django.urls import include, path

urlpatterns = [
  # others patterns
  path("", include("customers.urls", namespace="customers")),
]

Yep, got that:

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

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

The startapp command you would have used on page 1 should have also created an apps.py file in your polls directory. What are the contents of that file?

Also, what version of Django are you using? (Your error message implies you’re using Python 3.10, I’m just looking to confirm that you’re using a compatible version of Django.)

apps.py:

from django.apps import AppConfig


class PollsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'polls'

python -m django --version
4.1.3

What is listed in your INSTALLED_APPS setting?

Voilà

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

And just for confirmation - you haven’t made any changes to any of these files without exiting and restarting the shell, correct?
(Or, phrasing it another way, if you exit and restart the shell, this same error still occurs?)

If you start up runserver, does the url http://<whatever>/polls/ show you the index view?

If you enter an invalid URL, Django produces an error page showing all urls searched - can you enter an arbitrary URL to show what it can look for?

What else is in your polls/urls.py file?

Thanks @KenWhitesell for the hints. I will keep the hints in mind for future reference. But it turns out as far as I can tell I had a function or two that were not properly indented. How sweet. :wink: