Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31994 closed Bug (invalid)

Error in Tutorial 2

Reported by: Agorar101 Owned by: nobody
Component: Documentation Version: 3.1
Severity: Normal Keywords: Tutorial 2, TypeError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

The following function produces an error in the Tutorial 2. It can be problematic for new users that are getting acquainted with Django and do not know how to fix this issue.

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

returns the following error:

TypeError: can't compare datetime.datetime to datetime.date

It is easily fixed invoking the date method from now in the following way.

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now().date() - datetime.timedelta(days=1)

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDocumentation
Description: modified (diff)
Resolution: invalid
Status: newclosed

You've probably made a mistake somewhere, because pub_date is a DateTimeField so tutorial works properly. Closing per TicketClosingReasons/UseSupportChannels.

Last edited 4 years ago by Mariusz Felisiak (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top