Opened 2 years ago

Closed 2 years ago

Last modified 13 months 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 follow-up: Changed 2 years ago by ramiro

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

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 2 years ago by ramiro (previous) (diff)

comment:2 Changed 2 years ago by anonymous

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

comment:3 Changed 2 years ago by anonymous

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

comment:4 Changed 2 years ago 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.

comment:5 in reply to: ↑ 1 Changed 2 years ago by nilu214

class Poll(models.Model):

# ...
def was_published_recently(self):

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

comment:6 Changed 22 months ago 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

comment:7 Changed 13 months ago 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.

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