I suggest to fix the documentation at
https://docs.djangoproject.com/en/1.5//intro/tutorial02/
#-------------------------------------------------------------------------------------------
You can improve that by giving that method (in models.py) a few
attributes, as follows:
from django.utils import timezone # fix erorr "global name 'timezone' is
not defined"
import datetime # fix
class Poll(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() -
datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
#---------------------------------------------------------------------------------------------
--
Ticket URL: <https://code.djangoproject.com/ticket/19793>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => wontfix
* needs_tests: => 0
* needs_docs: => 0
Comment:
That is already done in part 1 of the tutorial, quoting:
''Note these are normal Python methods. Let’s add a custom method, just
for demonstration:''
{{{
import datetime
from django.utils import timezone
# ...
class Poll(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() -
datetime.timedelta(days=1)
}}}
''Note the addition of import datetime and from django.utils import
timezone, to reference Python’s standard datetime module and Django’s
time-zone-related utilities in django.utils.timezone, respectively. If you
aren’t familiar with time zone handling in Python, you can learn more in
the time zone support docs.''
We could add it to part two too, but there is a point in which trying to
support readers that attack the tutorial starting e.g. part two without
having following part one with isn't practical because we would have to
add that kind of notes in more places in detriment of readability.
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:1>
Comment (by anonymous):
In my opinion, the code should be used immediately after copy/paste...
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:2>
Comment (by anonymous):
In my opinion, the code should be working immediately after copy/paste...
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:3>
Comment (by aaugustin):
This argument doesn't hold: even with your suggestion, the code won't be
working immediately after copy/paste, since the field definitions aren't
included.
For the sake of readability the tutorial doesn't repeat the whole content
of every file for each edit. You have to follow steps in order.
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:4>
Comment (by nilu214):
class Poll(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= (timezone.now() -
datetime.timedelta(days=1))
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:5>
Comment (by anonymous):
I know this is an older thread. I had the same problem because I forgot
to add this line:
from django.utils import timezone
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:6>
Comment (by gmunumel):
The line above should be added in the poll's view. For some reason you
won't get the timezone error over the view until you run the tests.
--
Ticket URL: <https://code.djangoproject.com/ticket/19793#comment:7>