pytz is used in the Django version: <=3.2 doc Selecting the current time zone as shown below:
import pytz # Here
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')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
return self.get_response(request)
But, zoneinfo is used instead of pytz in the Django version: 4.0<= doc Selecting the current time zone as shown below:
import zoneinfo # Here
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')
if tzname:
timezone.activate(zoneinfo.ZoneInfo(tzname))
else:
timezone.deactivate()
return self.get_response(request)
My questions:
- Is
pytzdeprecated now or in the future in Python? - Why isn't
pytzused in the Django version: 4.0<= doc Selecting the current time zone?
To quote
pytz's README:Without using the literal word "deprecated", the package maintainer is proposing that you don't use
pytzfor new projects, so that's really deprecation for all intents and purposes.So Django's strategy is consistent with that, and in fact Django goes a bit further by facilitating the use of
zoneinfoin Python 3.8 (the oldest Python version still supported). From the Django 4.0 documentation you cited:and