Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#14052 closed (invalid)

Error on a code in Tutorial 01

Reported by: Wagner Macedo Owned by: nobody
Component: Documentation Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm learning Python and Django, and in tutorial 01, on url http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01, I think the code that's adding a custom method in the Poll class is wrong. Where is written:

import datetime
# ...
class Poll(models.Model):
    # ...
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

should be:

import datetime
# ...
class Poll(models.Model):
    # ...
    def was_published_today(self):
        return self.pub_date.date() == datetime.today().date()

The error is in the order today date invocation.

Sorry for my poor english, I'm brazillian.

Change History (2)

comment:1 by Karen Tracey, 14 years ago

Resolution: invalid
Status: newclosed

The tutorial code is correct. Try it in a Python shell:

>>> import datetime
>>> datetime.date.today()
datetime.date(2010, 8, 3)

The proposed change does not work:

>>> import datetime
>>> datetime.today().date()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'today'
>>>

Using the today() constructor of the datetime.datetime class instead of trying it on the datetime module (yes, this is a bit confusing in Python) would work:

>>> import datetime
>>> datetime.datetime.today().date()
datetime.date(2010, 8, 3)
>>>

But the existing code is simpler and works, so is preferable.

comment:2 by Wagner Macedo, 14 years ago

Sorry for annoy by this ticket. The tutorial code is right, but with me didn't work because I was using Eclipse to edit the code and the autocompletion of datetime added incorrectly the import

from django.utils.datetime_safe import datetime

that uses datetime instance

Note: See TracTickets for help on using tickets.
Back to Top