django-imagekit: Error when trying to access width/height after url in templates
Error (see traceback below) occurs if you generate image with assignment tag and then access width or height after url. If you use width/height before url - everything goes fine. Also error disappears if you try to access already generated images.
All the related settings are the default ones.
Examples:
from django.template import Template, Context
tpl = "{% load imagekit %}{% generateimage 'spec_name' source=obj.photo as im %} {{ im.url }} {{ im.width }}"
Template(tpl).render(Context({'obj': obj})) # error
# but no error for this:
tpl = "... {{ im.width }} {{ im.url }}"
# and also no error if thumb was already generated before render call
It’s quite strange and I’m not sure if that problem existed before because even in docs there’s an example that is doing exactly the same things.
Traceback:
.../lib/python3.4/site-packages/django/core/files/images.py in _get_width(self)
16 """
17 def _get_width(self):
---> 18 return self._get_image_dimensions()[0]
19 width = property(_get_width)
20
.../lib/python3.4/site-packages/django/core/files/images.py in _get_image_dimensions(self)
26 if not hasattr(self, '_dimensions_cache'):
27 close = self.closed
---> 28 self.open()
29 self._dimensions_cache = get_image_dimensions(self, close=close)
30 return self._dimensions_cache
.../lib/python3.4/site-packages/imagekit/files.py in open(self, mode)
57 def open(self, mode='rb'):
58 self._require_file()
---> 59 self.file.open(mode)
60
61 def _get_closed(self):
.../lib/python3.4/site-packages/django/core/files/base.py in open(self, mode)
139 self.file = open(self.name, mode or self.mode)
140 else:
--> 141 raise ValueError("The file cannot be reopened.")
142
143 def close(self):
PS. Confirmed for ImageKit 3.3 / Django 1.8-1.9 / Python 3.4.3
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 19 (3 by maintainers)
Commits related to this issue
- Fixed #350: Error when trying to access width/height after url If the file is closed and something is calling `open` now the file will be opened correctly event if it was already closed — committed to vstoykov/django-imagekit by vstoykov 8 years ago
- Merge pull request #384 from vstoykov/fix-350 Fixed #350: Error when trying to access width/height after url — committed to matthewwithanm/django-imagekit by vstoykov 8 years ago
- Merge branch 'release/4.0' * release/4.0: Bump version to 4.0 Universal wheels! Replaces #301 autodiscover works with AppConfig Ignore autogenerated CTags file Do not try south modelinspect... — committed to matthewwithanm/django-imagekit by vstoykov 7 years ago
@Terr, exactly! The simple workaround is to use
width
beforesrc
like<img width="{{ img.width }}" src="{{ img.url }}">
but sometimes it’s not handy (or even possible) because it can be used in different tags etc. Also I’ve found that this issue doesn’t appear ingenerate
tag without assignment only because its attributes is filled with dimension earlier (before src). I’ve swapped the lines and got the same error.Had the same issue with 3.3, I don’t use the template tag. Downgrading to 3.2.6 seems the solution.
I investigated the issue and found the line causing the problem.
When image is generated then temporary file is created and attached to the
ImageCacheFile
instance to prevent call to the storage backend on next access thefile
attribute. The problem is that this temporary file is removed from the file system when it is closed and when django try to reopen it then it raise an error.I will try to see a better way of generating thumbnails that will fix the issue but if possible without extra calling the storage backend for performance reason.
For now you can use the workaround with putting the
width
before thesrc
in the markup until fix is ready.