Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14052 closed (invalid)

Error on a code in Tutorial 01

Reported by: wagnerluis1982 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: UI/UX:

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.

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 4 years ago by wagnerluis1982

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.