1. David Larlet
  2. django-storages
  3. Issues


Issue #179 resolved

AttributeError 'ImageFieldFile' object has no attribute 'startswith'

created an issue

Using django-storages 1.1.8 and have a model 'product' with an ImageFileField 'logo' (in Django 1.5.1). My template uses {% static product.logo %}, which works fine in development not using django-storages, but setting up staging and using django-storages I get the above traceback rendering the template in posixpath.py in normpath for path.startswith('/').

Staticfiles.py calls in line 12, return staticfiles_storage.url(path), where path is the ImageFileField and Django normally handles this fine using django.utils.encoding.filepath_to_uri, but in storages/backends/s3boto.py url, _clean_name is called which appears to assume that path (called name in s3boto.py) is a string. If I add: if name.class.name == 'ImageFieldFile': name = name.unicode() at line 458 in s3boto.py, for the definition of url(self, name), it works--which is obviously not general to all the potential issues here with different types of FieldFiles.

Thanks--and let me know if I am missing something obvious.

Comments (6)

  1. dasloss reporter

    Another quick fix is using product.logo|stringformat('s') in the template to make sure only a string is passed in--though obviously this shouldnt have to be done.

  2. Ian Lewis

    In the current tip we call filepath_to_uri() so I think this should be ok.

    That said, I think the path name passed to a storage backend is generally accepted to be a string/unicode object rather than just any old object. I think that you should be using {{ product.logo.url }} (It might be {{ product.logo.file.url }}) in your template rather than the {% static %} template tag.

    This is especially true because static files and uploaded files are often served separately and uploaded files are joined with the MEDIA_URL rather than STATIC_URL like the static template tag does.

  3. Log in to comment