Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#19793 closed Cleanup/optimization (wontfix)

global name 'timezone' is not defined

Reported by: anonymous Owned by: nobody
Component: Documentation Version: 1.5-alpha-1
Severity: Normal Keywords: global name 'timezone' is not defined
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

NameError at /admin/polls/poll/
global name 'timezone' is not definedRequest Method: GET
Request URL: http://127.0.0.1:8000/admin/polls/poll/
Django Version: 1.5c1
Exception Type: NameError
Exception Value: global name 'timezone' is not defined
Exception Location: ...../polls/models.py in was_published_recently, line 14
Python Executable: /usr/bin/python3
Python Version: 3.2.3
############################################################################################################

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?'

#---------------------------------------------------------------------------------------------

Change History (7)

comment:1 by Ramiro Morales, 12 years ago

Resolution: wontfix
Status: newclosed

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 at arbitrary places e.g. part two without having followed part one isn't practical because it is detrimental to readability.

Last edited 12 years ago by Ramiro Morales (previous) (diff)

comment:2 by anonymous, 12 years ago

In my opinion, the code should be used immediately after copy/paste...

comment:3 by anonymous, 12 years ago

In my opinion, the code should be working immediately after copy/paste...

comment:4 by Aymeric Augustin, 12 years ago

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.

in reply to:  1 comment:5 by nilu214, 11 years ago

class Poll(models.Model):

# ...
def was_published_recently(self):

return self.pub_date >= (timezone.now() - datetime.timedelta(days=1))

comment:6 by anonymous, 11 years ago

I know this is an older thread. I had the same problem because I forgot to add this line:
from django.utils import timezone

comment:7 by Gabriel Muñumel, 10 years ago

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.

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