In “Writing Your First Django App, Part 5”, in the section “Django Test Client” I am getting the following Traceback Error when entering the command response = client.get(reverse("polls:index")
in the shell:
Internal Server Error: /polls/
Traceback (most recent call last):
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
response = response.render()
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 114, in render
self.content = self.rendered_content
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 90, in rendered_content
template = self.resolve_template(self.template_name)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 72, in resolve_template
return select_template(template, using=self.using)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py", line 47, in select_template
raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: polls/index.html, polls/question_list.html
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\test\client.py", line 927, in get
response = super().get(path, data=data, secure=secure, headers=headers, **extra)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\test\client.py", line 457, in get
return self.generic(
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\test\client.py", line 609, in generic
return self.request(**r)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\test\client.py", line 891, in request
self.check_exception(response)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\test\client.py", line 738, in check_exception
raise exc_value
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
response = response.render()
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 114, in render
self.content = self.rendered_content
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 90, in rendered_content
template = self.resolve_template(self.template_name)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 72, in resolve_template
return select_template(template, using=self.using)
File "C:\Users\duysa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py", line 47, in select_template
raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: polls/index.html, polls/question_list.html
When I enter response.status_code
after this, it still returns 404
and not the expected 200
Here is my models.py:
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
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
def test_was_published_recently_with_old_question(self):
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
old_question = Question(pub_date=time)
self.assertIs(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self):
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
self.assertIs(recent_question.was_published_recently(), True)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text