I'm currently working my way through the Django tutorial and I'm having trouble getting one of the tests from chapter 5 to pass.
Specifically this one:
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)
I see the response does contain 'Past question'. But it's stored under response -> context_data -> 'question'
Here's the function for create 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)
The Question model:
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?'
And the view:
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())
If it helps, the Git repo with everything can be found here.
Thanks in advance for the help!
Aucun commentaire:
Enregistrer un commentaire