Python Django教程第5部分错误
我在学习Django编程 我在玩第五部分 我会把地址留在下面 我添加了测试文件的内容 但是我做了测试,但是测试文件有问题 我试着找一个打字错误,但找不到 有什么问题 这是回溯 回溯最近一次呼叫上次: 文件/home/choco/python3/django/mysite/polls/tests.py,第96行,在两个过去的问题中 创建问题文本=过去的问题1,天数=-30 NameError:未定义名称“创建问题” ----------------------------------- 在0.103秒内运行了8次测试 失败错误=5Python Django教程第5部分错误,python,django,Python,Django,我在学习Django编程 我在玩第五部分 我会把地址留在下面 我添加了测试文件的内容 但是我做了测试,但是测试文件有问题 我试着找一个打字错误,但找不到 有什么问题 这是回溯 回溯最近一次呼叫上次: 文件/home/choco/python3/django/mysite/polls/tests.py,第96行,在两个过去的问题中 创建问题文本=过去的问题1,天数=-30 NameError:未定义名称“创建问题” ----------------------------------- 在0.1
正在销毁别名“default”的测试数据库…您已缩进create_question,以便它位于QuestionModelTests类中。我认为您不是有意这么做的-它不在该类中使用,也不引用任何类属性。您应该取消对它的登录,以便它位于所有类之外。您需要了解有关OOP的更多信息。检查此代码并更正:
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_old_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
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):
"""
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
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)
def test_was_published_recently_wiht_future_question(self):
"""
test_was_published_recently() returns False for questions whose pub_date
is in the test_was_published_recently_wiht_future_question
"""
time = timezone.now() +datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
def create_question(self, question_text, days):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text, pub_date=time)
class QuestionIndexViewTests(TestCase):
def create_question(self, question_text, days):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text, pub_date=time)
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse('polls:index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No polls are available.")
self.assertQuerysetEqual(response.context['latest_question_list'], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
self.create_question(question_text="Past question.", days=-30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question.>']
)
def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""
self.create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))
self.assertContains(response, "No polls are available.")
self.assertQuerysetEqual(response.context['latest_question_list'], [])
def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
self.create_question(question_text="Past question.", days=-30)
self.create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question.>']
)
def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
self.create_question(question_text="Past question 1.", days=-30)
self.create_question(question_text="Past question 2.", days=-5)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question 2.>', '<Question: Past question 1.>']
)