django's timezone.now does not show the right

2019-03-08 22:17发布

问题:

My server is in London.

In my settings.py I have:

TIME_ZONE = 'Europe/Moscow'
USE_TZ = True

But when I do this:

from django.utils import timezone

print timezone.now().hour

It prints London's time. What do I do wrong?

UPDATE:

>> timezone.now()
datetime.datetime(2013, 4, 16, 12, 28, 52, 797923, tzinfo=<UTC>)

tzinfo = <UTC>, so maybe it prints not London time, but UTC's +0 time? Anyway, how to make django show Moscow time?

Also, when I render template with now = timezone.now().

{{ now.hour }} prints, for example, 12 (London time)

{{ now|date:"G" }} prints 16 (Moscow time)

回答1:

See question #2 in the "Usage" section of the Django docs.

>>> from django.utils import timezone
>>> timezone.localtime(timezone.now())