the django tutorial part 5 the python shell returns "TypeError: 'NoneType' object is not subscriptable" but should return "<QuerySet[<...>]>

I am currently doing the official django tutorial.
https://docs.djangoproject.com/en/4.1/intro/tutorial05/#the-django-test-client

This is the expected return of the python shell

>>> 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'))
>>> response.status_code
200
>>> response.content
b'\n    <ul>\n    \n        <li><a href="/polls/1/">What&#x27;s up?</a></li>\n    \n    </ul>\n\n'
>>> response.context['latest_question_list']
<QuerySet [<Question: What's up?>]>

But this is what it looks like for me:

>>> 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'))
>>> response.status_code
200
>>> response.content
b'\n    <ul>\n        \n        <li><a href="/polls/2/">What&#x27;s going on?</a></li>\n        \n        <li><a href="/polls/1/">What&#x27;s new?</a></li>\n        \n    </ul>\n'
>>> response.context['latest_question_list']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

Instead of the TypeError I expect a QuerySet! Now what can help here? I will include the polls/models.py and polls/views.py here:

polls/views.py

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic

from .models import Choice, Question
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))




polls/models.py

from django.db import models
import datetime
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now


class Choice(models.Model):
    def __str__(self):
        return self.choice_text

    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

I tried backtracing the tutorial looking for any errors. Instead of typing myself what the tutorial says as I usually do, I just copy pase like a monkey for solving the error but still it doesnt help

What is your urls.py definition for all paths with the name index?

this is under polls/urls.py

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

When you’re first entering the shell, are you entering these commands?

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()

These need to be entered each time you start the shell.

Thank you so much, ken you also provided my other question with a solution youre the best ken

Also I think someone should really look into that in the tutorial because the tutorial doesnt state that you should first create the testenvironment, on part 5 at least

Yes it does. It’s the first example in that section (after starting the shell) at Writing your first Django app, part 5 | Django documentation | Django. It also explains why it’s necessary to do that. (The lines I posted are quoted directly from it.)

ouch how embarassing
thanks