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

时间:2015-03-07 18:23:03

标签: django testing

我目前正在通过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)

我看到回复确实包含过去的问题'。但它存储在响应中 - > context_data - > '问题'

这里是创建问题的功能:

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在文件templates/polls/detail.html的模板中,您有一个拼写错误:

你应该替换:

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

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