Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'datetime.timezone' object has no attribute '_utcoffset' #73

Closed
tvandraren opened this issue May 9, 2022 · 1 comment

Comments

@tvandraren
Copy link

tvandraren commented May 9, 2022

I'm trying to execute this code from a 2 year-old project:

import pytz
from django.utils import timezone

pytz.timezone('Europe/Madrid').normalize(timezone.now())

and it suddenly gives me this:

Traceback (most recent call last):
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\views\generic\base.py", line 84, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\views\generic\base.py", line 119, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\Mitios\weekplan\views.py", line 115, in get
    return render(
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\backends\django.py", line 62, in render
    return self.template.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
    return self.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
    return self.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
    return self.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\loader_tags.py", line 208, in render
    return template.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 177, in render
    return self._render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 958, in render_annotated
    return self.render(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\defaulttags.py", line 315, in render
    match = condition.eval(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\defaulttags.py", line 878, in eval
    return self.value.resolve(context, ignore_failures=True)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 712, in resolve
    obj = self.var.resolve(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 842, in resolve
    value = self._resolve_lookup(context)
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\django\template\base.py", line 909, in _resolve_lookup
    current = current()
  File "C:\Users\tvand\Documents\repos\Mitios\weekplan\models.py", line 28, in es_atrasada
    fecha_asignacion = self._fecha_huso(self.fecha_asignacion)
  File "C:\Users\tvand\Documents\repos\Mitios\weekplan\models.py", line 44, in _fecha_huso
    pytz.timezone('Europe/Madrid').normalize(timezone.now())
  File "C:\Users\tvand\Documents\repos\Mitios\venv\lib\site-packages\pytz\tzinfo.py", line 252, in normalize
    offset = dt.tzinfo._utcoffset

I'm using:

Django==4.0.3
pytz==2022.1

I checked that django.utils.timezone.tzinfo does have a utcoffset. Has there been a change on pytz concerning this or is it Django's fault? Any idea on how to solve it?

@stub42
Copy link
Owner

stub42 commented Aug 12, 2022

pytz methods only work with pytz tzinfo instances. timezone.now() is not returning a pytz tzinfo instance. I don't know what it is, so cannot suggest how to convert between the two. The Django end may have been updated to use the new timezone support built into Python, making most of pytz obsolete.

@stub42 stub42 closed this as completed Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants