17

When I see dates and times in the admin, they are displayed in UTC. I'd like for them to be displayed in my local timezone. I checked the TIME_ZONE setting in the docs, though it doesn't look like that is quite what I want. TIME_ZONE determines the time zone for the datetimes stored in the database, which is not what I want to set -- I just want to localize the time zones for the admin, but not change how they are saved at the database level.

Is there a way to do this?

orange1
  • 2,871
  • 3
  • 32
  • 58

4 Answers4

10

In Django:

The default time zone is the time zone defined by the TIME_ZONE setting.

The current time zone is the time zone that’s used for rendering.

so you should set the "current time zone" use:

django.utils.timezone.activate(timezone)

You can activate it in a middleware(don't forger to register it to MIDDLEWARE in settings.py):

import pytz    
from django.utils import timezone

class TimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        timezone.activate(pytz.timezone('Asia/Shanghai')
        return self.get_response(request)

This is only a simple hardcode. you can making a form to select tzinfo, store it in user's profile and load it to user session, read the tzinfo from the session in the middleware.

The official documentation has a related description: https://docs.djangoproject.com/en/3.0/topics/i18n/timezones/#selecting-the-current-time-zone

Reference:

https://groups.google.com/forum/#!topic/django-developers/E4aiv3O6vGo

tinyhare
  • 2,271
  • 21
  • 25
  • In Python 3.9+, you do not need `pytz` and can pass either a string to `activate` or a `zoneinfo.ZoneInfo` object. – pphysch Apr 18 '22 at 17:32
4

This isn't supported natively in Django as of 2.1. However https://github.com/charettes/django-sundial may provide what you need -- this adds a User.timezone model field so that you can configure it per-user.

(This middleware is very simple -- under the covers it's just calling timezone.activate(zone) to change the rendered site's timezone).

Symmetric
  • 4,495
  • 5
  • 32
  • 50
2

You can override the template for your change_list.html, change_form.html, etc. and do something like:

{% extends "admin/change_list.html" %}
{% load tz %}

{% block content %}
    {% timezone "US/Eastern" %}
        {{ block.super }}
    {% endtimezone %}
{% endblock %}

Do it with one of those methods:

fjsj
  • 10,995
  • 11
  • 41
  • 57
  • I follow this but for certain forms it seems to update the datetime without the timezone offset. Have you noticed this? – Harry Moreno Feb 01 '21 at 03:09
  • It's been a while since a last tried this. Anything special about those forms or fields that are different? – fjsj Feb 02 '21 at 14:37
  • nope, If you follow the directions you gave you'll notice that the date edit fields accept UTC. So to sidestep the issue I just make sure to edit ALL dates in UTC before saving the model. – Harry Moreno Feb 02 '21 at 15:49
-9

Maybe you can try to set user timezone. Set

 USE_TZ = True

Official documentation

鄢志昊
  • 1
  • 2
  • I already have it set to true -- that setting controls whether or not one is using timezone-aware dates. – orange1 Mar 07 '17 at 18:33