[Fixed]-DateTimeField received a naive datetime

42👍

Further to falsetru’s answer, if the datetime has already been created you can convert it to timezone aware:

from django.utils import timezone
my_datetime = timezone.make_aware(my_datetime, timezone.get_current_timezone())

24👍

Use django.utils.timezone.now instead of datetime.datetime.now.

from django.utils import timezone
current_time = timezone.now()

3👍

You can also make the datetime time zone aware with localize from pytz, as explained here.

UTC:

import pytz
dt_aware = pytz.utc.localize(dt_naive)

Any other time zone:

import pytz
tz = 'Europe/Berlin' #or whaterver timezone you want
dt_aware = pytz.timezone(tz).localize(dt_naive)

And here the list of timezones.

👤J0ANMM

Leave a comment