0
1

Previously, this issue I was searched how to fix this for a long time.
And finally I found the best solution how to fix the timezone.localtime for django.

In fact, if you enable the USE_TZ = True in your settings.py and timezone of your computer machine not same with your django app settings.TIME_ZONE. It will make you confuse, because when you using the django queryset you should convert the datetime from client into activated django timezone. Another case, when you try to get the datetime field in the object. It will return datetime with timezone UTC.

By default django.utils.timezone.now will return datetime.datetime.
and datetime.datetime adjust the timezone condition on the computer being used.
For example:

# IN MY LOCAL MACHINE
$ ~ timedatectl status | grep "Time zone"
       Time zone: Asia/Jakarta (WIB, +0700)



# IN MY SERVER MACHINE
$ ~ ssh xxxxx.com
[email protected]:~$ timedatectl status | grep "Time zone"
       Time zone: Etc/UTC (UTC, +0000)

Solution

You should setup the timezone in your server machince (vps, hosting, etc) with your current timezone, eg: Asia/Jakarta.

# direct server
sudo timedatectl set-timezone Asia/Jakarta

# or

# docker
RUN ln -s -f /usr/share/zoneinfo/Asia/Jakarta /etc/localtime

and then disable the timezone in your django app

USE_TZ = False

So, I think this the best solution to handle it.

Refferences:

server linux django tricks settings solution problem

Your Answer

blog comments powered by Disqus