0

I have a model like this:

class Entry(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    text = models.TextField()   
    date_added = models.DateTimeField(default=timezone.now())

The date_added displayed in the template:

{% localtime on %}
  {{ entry.date_added|date:'d M, Y H:i'}}  
{% endlocaltime %}

As suggested in https://docs.djangoproject.com/en/3.0/topics/i18n/timezones.

In my setting.py, my USE_TZ = True

However, the output gives medatetime.datetime(2020, 8, 22, 9, 20, 16, 439533, tzinfo=<UTC>) or 22 Aug, 2020 9:20 in my webpage

Why does the output be in UTC? In my understanding, aware datetime means it follows the user's timezone. My timezone is UTC+7, so it must be 22 Aug 2020 16:20.

I've read Retrieve timezone aware DateTimeField in Django that suggests changing TIME_ZONE = but wouldn't that makes it unaware?

What can I do to fix it? I expect the datetime follows the user's timezone

Thank you

2 Answers2

0

In settings.py file please set the following.

TIME_ZONE = 'America/Los_Angeles'
USE_TZ = False
Riyas Ac
  • 1,553
  • 1
  • 9
  • 23
0

Yes you need to keep USE_TZ = True.

The TIME_ZONE setting is simply the default for your application.

To allow users to change their timezone you need to provide them with the ability to do so in your app. Once they have changed their timezone you use timezone.activate() to make their choice active.

See this small example from the docs for more information.

romaingz
  • 421
  • 2
  • 6