Thursday, January 9, 2014

Django 1.6 global name 'timezone' is not defined

If you are following the documentation to run the sample app, while doing date time comparison you might run into the issue on following code -

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

It complains - global name 'timezone' is not defined

because it needs explicit import -

import datetime
from django.utils import timezone

Once you add it should be resolved. 

No comments:

Post a Comment