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:
pytz deprecated now or in the future in Python?pytz used in the Django version: 4.0<= doc Selecting the current time zone?To quote pytz's README:
This project is in maintenance mode. Projects using Python 3.9 or later are best served by using the timezone functionaly now included in core Python and packages that work with it such as tzdata.
Without using the literal word "deprecated", the package maintainer is proposing that you don't use pytz for 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 zoneinfo in Python 3.8 (the oldest Python version still supported). From the Django 4.0 documentation you cited:
Time zone support uses
zoneinfo, which is part of the Python standard library from Python 3.9. Thebackports.zoneinfopackage is automatically installed alongside Django if you are using Python 3.8.
and
Changed in Django 4.0:
zoneinfowas made the default timezone implementation. You may continue to usepytzduring the 4.x release cycle via theUSE_DEPRECATED_PYTZsetting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With