Django timezone doesn't show the right time?

710 views
Skip to first unread message

Dan Santos

unread,
Apr 30, 2012, 4:03:25 PM4/30/12
to Django users
Hi I'm a total programming newbie!

### INTRO ###
I have been following this tutorial and the timezone outputs confuses
me.
https://docs.djangoproject.com/en/1.4/intro/tutorial01/#playing-with-the-api

And I'm still confused after reading these explanations, I basically
need some really dumbed down answers to understand this timezone
business:

<b>TIME_ZONE setting: How does it work?</b>
http://groups.google.com/group/django-users/browse_thread/thread/bebbc1310f24e945/100d0fc3e8620684?lnk=gst&q=timezone+problems#



### QUESTION ###
* Europe/Brussels has UTC+2 during summer time.

When I run this as I follow the Django tutorial, then the time (20:34)
is behind by 2 hours to my local time and UTC displays +00:00.
Shouldn't it display 22:34 +02:00 instead for a server located at
Europe/Brussels?

>>> Poll.objects.all()
<Poll: How do you do? 2012-04-26 20:34:33.337247+00:00>]

akaariai

unread,
May 1, 2012, 9:16:33 AM5/1/12
to Django users
On Apr 30, 11:03 pm, Dan Santos <dansanto...@gmail.com> wrote:
> Hi I'm a total programming newbie!
>
> ### INTRO ###
> I have been following this tutorial and the timezone outputs confuses
> me.https://docs.djangoproject.com/en/1.4/intro/tutorial01/#playing-with-...
>
> And I'm still confused after reading these explanations, I basically
> need some really dumbed down answers to understand this timezone
> business:
>
> <b>TIME_ZONE setting: How does it work?</b>http://groups.google.com/group/django-users/browse_thread/thread/bebb...
>
> ### QUESTION ###
> * Europe/Brussels has UTC+2 during summer time.
>
> When I run this as I follow the Django tutorial, then the time (20:34)
> is behind by 2 hours to my local time and UTC displays +00:00.
> Shouldn't it display 22:34 +02:00 instead for a server located at
> Europe/Brussels?
>
> >>> Poll.objects.all()
>
> <Poll: How do you do? 2012-04-26 20:34:33.337247+00:00>]

Internally Django works in UTC . When you display a value in a
template or in a form it will be converted to the currently active
time zone (by default settings.TIMEZONE). So, when you just do
Poll.objects.all() you will see the UTC time, as this is Python
internal representation. If you would do {{poll.pub_date}} in the
template, it would be displayed in the time zone you have currently
active.

- Anssi

Dan Santos

unread,
May 1, 2012, 2:45:43 PM5/1/12
to django...@googlegroups.com
Hi thanks for taking the time to explain to me!

Unfortunately I still don't understand the outputs that I get in Python console, maybe it's because I don't understand the concept of templates.
But this is what I tried to do in Python console.

>>> p=Poll(question="Giddeup'ah!", pub_date=timezone.now())
>>> p.save()
>>> p.id
4
>>> p.question
"Giddeup'ah!"
>>> p.pub_date
datetime.datetime(2012, 5, 1, 18, 25, 39, 804854, tzinfo=<UTC>)

>>> Poll.objects.all()
[<Poll: Waddup'ah? 2012-04-26 08:39:20.605340+00:00>, <Poll: What's up? 2012-04-26 10:30:23.624835+00:00>, 
<Poll: How do you do? 2012-04-26 20:34:33.337247+00:00>, <Poll: Giddeup'ah! 2012-05-01 18:25:39.804854+00:00>]


p.pub_date and Poll.objects.all() show the same time info, so how do I see the time with the simplest of templates?



Also when I tried typing "poll.pub_date" in different ways I get these errors.

>>> Poll.pub_date
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Poll' has no attribute 'pub_date'

>>> poll.pub_date
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'poll' is not defined

>>> poll.pub_date()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'poll' is not defined

>>> Poll.pub_date()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Poll' has no attribute 'pub_date'


Here's a better overview of what my relevant files looks like and what setup I have tried to use.  Maybe it will help in finding if I have mis-configured something.

akaariai

unread,
May 1, 2012, 3:06:42 PM5/1/12
to Django users
I see I am talking about concepts which are not yet familiar to you.

By template I mean Django's HTML template engine. This is introduced
in part 3 of the tutorial.

The reason for the errors is that Poll is not an instance, it is the
class so it doesn't have pub_date. poll isn't assigned to yet. You
could do poll = Poll.objects.all()[0], and then poll would be assigned
the first Poll object instance, fetched from the database.

- Anssi

On May 1, 9:45 pm, Dan Santos <dansanto...@gmail.com> wrote:
> Hi thanks for taking the time to explain to me!
>
> Unfortunately I still don't understand the outputs that I get in Python
> console, maybe it's because I don't understand the concept of templates.
> But this is what I tried to do in Python console.
>
> >>> p=Poll(question="Giddeup'ah!", pub_date=timezone.now())
> >>> p.save()
> >>> p.id
> 4
> >>> p.question
> "Giddeup'ah!"
> >>> *p.pub_date*
>
> datetime.datetime(2012, 5, 1, *18, 25*, 39, 804854, tzinfo=<*UTC*>)
>
> >>> *Poll.objects.all()*
>
> [<Poll: Waddup'ah? 2012-04-26 08:39:20.605340+00:00>, <Poll: What's up?
> 2012-04-26 10:30:23.624835+00:00>,
> <Poll: How do you do? 2012-04-26 20:34:33.337247+00:00>, <Poll: Giddeup'ah!
> 2012-05-01 *18:25*:39.804854*+00:00*>]
>
> p.pub_date and Poll.objects.all() show the same time info, so how do I see
> the time with the simplest of templates?
>
> Also when I tried typing "poll.pub_date" in different ways I get these
> errors.
>
> >>> *Poll.pub_date*
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: type object 'Poll' has no attribute 'pub_date'
>
> >>> *poll.pub_date*
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> NameError: name 'poll' is not defined
>
> >>> *poll.pub_date()*
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> NameError: name 'poll' is not defined
>
> >>> *Poll.pub_date()*
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: type object 'Poll' has no attribute 'pub_date'
>
> Here's a better overview of what my relevant files looks like and what
> setup I have tried to use.  Maybe it will help in finding if I
> have mis-configured something.http://www.python-forum.org/pythonforum/viewtopic.php?f=19&t=34082

Dan Santos

unread,
May 2, 2012, 12:33:05 PM5/2/12
to django...@googlegroups.com
Thanks!  I guess that I will see the light once I get to part 3 of the tutorial.  Thanks for the pointer now I understand the big picture.
Reply all
Reply to author
Forward
0 new messages