无法解决页面内容的 Django 测试

Having trouble solving a Django test for page content

我目前正在学习 Django 教程,但我无法通过第 5 章中的一项测试。

特别是这个:

    def test_detail_view_with_a_past_question(self):
        past_question = create_question(question_text='Past question.', days=-5)
        response = self.client.get(reverse('polls:detail', args=(past_question.id,)))
        self.assertContains(response, past_question.question_text, status_code=200)

我看到回复确实包含 'Past question'。但它存储在 response -> context_data -> 'question'

这是创建问题的函数:

def create_question(question_text, days):
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)

问题模型:

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

    def __unicode__(self):
        return self.question_text

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

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

和视图:

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

    def get_queryset(self):
        """
        Excludes any questions that aren't published yet.
        """
        return Question.objects.filter(pub_date__lte=timezone.now())

如果有帮助,包含所有内容的 Git 回购可以是 found here

在此先感谢您的帮助!

文件 templates/polls/detail.html 中的模板中有错字:

你应该替换:

<h1>{{ question.question_test }}</h1>

<h1>{{ question.question_text }}</h1>