Code

Opened 14 months ago

Closed 14 months ago

Last modified 6 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?'

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

Attachments (0)

Change History (6)

comment:1 follow-up: Changed 14 months 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 14 months ago by ramiro (previous) (diff)

comment:2 Changed 14 months ago by anonymous

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

comment:3 Changed 14 months ago by anonymous

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

comment:4 Changed 14 months 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 11 months 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 6 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

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.