[Django] #19793: global name 'timezone' is not defined

667 views
Skip to first unread message

Django

unread,
Feb 10, 2013, 7:53:49 AM2/10/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: 1.5-alpha-1
Component: Documentation | Keywords: global name 'timezone'
Severity: Normal | is not defined
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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?'

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

--
Ticket URL: <https://code.djangoproject.com/ticket/19793>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 10, 2013, 8:42:32 AM2/10/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ramiro):

* 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>

Django

unread,
Feb 12, 2013, 5:22:53 AM2/12/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 12, 2013, 5:25:52 AM2/12/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 12, 2013, 6:59:56 AM2/12/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
May 24, 2013, 10:18:33 PM5/24/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Oct 20, 2013, 10:26:14 AM10/20/13
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jul 22, 2014, 9:23:42 PM7/22/14
to django-...@googlegroups.com
#19793: global name 'timezone' is not defined
-------------------------------------+-------------------------------------
Reporter: anonymous | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version:
Component: Documentation | 1.5-alpha-1
Severity: Normal | Resolution: wontfix
Keywords: global name | Triage Stage:
'timezone' is not defined | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages