[Answer]-Problems while following the instructions of Django official documentation

1👍

According to the method name I would say that False is the correct return value in this case. The method should return True if the Question is less old than one day. In your case the pub_date is a date more older than one day so it was not “published recently” so the method returns False. If you change your date to now with q.pub_date = timezone.now() and then save with q.save(), q.was_published_recently() should return True for exactly one day.

0👍

At last, I have found the problem. It was in the definition of the was_published_recently() method. It should be <= instead of >=.

Here is the redefined method:

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

Previously, the method was testing if the pub_date is greater or equal than the yesterday which was wrong.

Now, it is testing if the pub_date is less or equal than the yesterday, which is right.

👤ni8mr

Leave a comment