Search code examples
pythondjangoboto3django-storage

Django 1.10: django-storages on S3 error: Not naive datetime (tzinfo is already set)


I've started getting an error since upgrading to Django 1.10. I'm running Django 1.10 on Python 3.5 with django-storages==1.5.0 and boto3==1.4.0.

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
    collected = self.collect()
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
    handler(path, prefixed_path, storage)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 337, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 258, in delete_file
    target_last_modified = self.storage.get_modified_time(prefixed_path)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/files/storage.py", line 231, in get_modified_time
    return _possibly_make_aware(dt)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/core/files/storage.py", line 243, in _possibly_make_aware
    return timezone.make_aware(dt, tz).astimezone(timezone.utc)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/django/utils/timezone.py", line 368, in make_aware
    return timezone.localize(value, is_dst=is_dst)
  File "/home/vagrant/.virtualenvs/wrds-classroom/lib/python3.5/site-packages/pytz/tzinfo.py", line 304, in localize
    raise ValueError('Not naive datetime (tzinfo is already set)')
ValueError: Not naive datetime (tzinfo is already set)

Changeing USE_TZ to False changes the error, but I'm still not sure why this is being caused (this is new territory in the Django codebase for me):

TypeError: can't compare offset-naive and offset-aware datetimes

Any idea what the cause here is?


Solution

  • UPDATE:

    I'm updating my own answer, as this pull request has now been merged. To fix, simply use version 1.5.1 in your requirements.txt or pip install command:

    django-storages==1.5.1
    

    OUT OF DATE ANSWER FOR CONTEXT:

    I've figured out a temporary fix which should lead to a permanent fix.

    After some more sleuthing, a friend found this PR they thought might be related: https://github.com/jschneier/django-storages/pull/181

    I noticed the date of the pull request was two days after the latest django-storages release (1.5.0) which I was running. In my requirements.txt I simply did this, pointing at the hash of the commit:

    #django-storages==1.5.0
    git+https://github.com/jschneier/django-storages.git#5f280571ee1ae93ee66ed805b53b08bfe5ab9f0c
    boto3==1.4.0
    

    Then, a pip install --upgrade -r requirements.txt and ran collectstatic again, and no more error! I'm guessing this fix will be included in a 1.5.1 release, at which point I'll simply change my requirements.txt again.