1

I am trying to work with the Django Timezone Middleware which I took from the offical documentation, but it doesn't seem to work.

I have a middleware.py file like this:

    import pytz

from django.utils import timezone

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

    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        print('DJANGO TIMEZONE', tzname)
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

Where I output the "tzname" to the console, it is set to None which must be the problem, but why isn't it set?

My settings file has the following:

USE_TZ = True
TIME_ZONE = "UTC"

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'myproject.middleware.TimezoneMiddleware',
]

My template looks like this:

{% load tz %}
{% get_current_timezone as TIME_ZONE %}
{{ TIME_ZONE }}


{% localtime on %}
    <p>{{ room.open_date }}</p>
{% endlocaltime %}

The result is that {{ room.open_date }} will show in UTC. {{ TIME_ZONE }} displays 'UTC'. I have tried without the TIME_ZONE setting in settings. I believe I have implemented it according to the documentation, but am obviously missing something. My Django version is Django version 3.2.5.

Please can someone help. My goal is to display the dates in the timezone that the browser has.

OptimusPrime
  • 777
  • 16
  • 25
  • I've used this below and so far it seems to be working. https://github.com/adamcharnock/django-tz-detect – OptimusPrime Jul 26 '21 at 06:32
  • 1
    It appears you've implemented the part that reads the timezone from the session, but not the part that actually stores it in the session. Read the [documentation](https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#selecting-the-current-time-zone) again and note that there are three parts: *Add the following middleware...*, *Create a view...*. and *Include a form...*. – Kevin Christopher Henry Jul 26 '21 at 16:29
  • I didn't create the view that sets the timezone, I think that's what it requires. I used the code from Adam Charnock which worked for me so this is something to think about for next time. – OptimusPrime Aug 01 '21 at 09:57

1 Answers1

0

Sorry that I can't put my answer as a comment, I made this answer that worked for me, I hope it can help you.

According to the documentation, the detail with the sessions is that you must set it, or failing that, if you want to obtain the timezone, you must request it from the user through a form. This solution seems to me to be an automatic way.

Django Timezone with Cookies & JS

Rogelio Carrera
  • 161
  • 1
  • 6