使用模板语法时,django-sorl 会产生 IOError

发布于 2024-10-20 21:43:45 字数 5925 浏览 3 评论 0原文

我已经设置了 django-sorl ,我真的认为我忽略了一些简单的事情。我不断收到以下错误(TEMPLATE_DEBUG = True 和 THUMBNAIL_DEBUG = True)

请求方法:GET 请求URL: http://localhost:8000/events/edit/1

Django 版本:1.2.4 异常类型: TemplateSyntaxError 异常值:

渲染时捕获 IOError:(2,“没有这样的文件或目录”)

异常位置: _open 中的 /usr/lib/python2.6/site-packages/django/core/files/storage.py,第 137 行

Python 可执行文件: /usr/bin/python Python 版本: 2.6.4

我正在运行一个redis服务器设置...我的sorl配置是:

settings.py -snip-:

TEMPLATE_DEBUG = DEBUG
THUMBNAIL_DEBUG = DEBUG
THUMBNAIL_KVSTORE = 'sorl.thumbnail.kvstores.redis_kvstore.KVStore'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'sorl.thumbnail',
)

models.py -snip-:

from sorl.thumbnail import ImageField
...
    path = ImageField(
       _('Image'),
        upload_to='event/%Y/%m/%d/%H',
    )
...

template.html -snip-:

{% load thumbnail %}
...
{% thumbnail image "200x100" as im %}
    <img src="{{ im.url }}" alt="{{ im.name }}"/>
{% endthumbnail %}
...

我想我已经接近了,因为文件正在被 在缓存文件夹中按需创建

sh> find static/cache/ -type f 
static/cache/db/ac/dbac605942d9651eb25f16cf05f5e672.jpg
static/cache/82/bc/82bcdd2ab079187c6b6110cb0e0c1505.jpg
static/cache/07/77/077784411739d4f8b1758255783388ec.jpg

当我打开这些文件时, ;它们是我按需请求的图像。试图打开图像显示它时似乎正在轰炸。另一件需要注意的事情是,如果我只是使用以下语法显示图像,它将正常工作(没有 sorl):

工作 template.html (没有 sorl - 因此......没有缩略图) -snip-:

<img src="{{ MEDIA_URL}}{{ image.path }}" alt="{{ image.name }}"/>

关键是;我确实需要为要显示的图像添加前缀 {{ MEDIA_URL }} 标记(我不确定这是否是我在 sorl 中遇到的错误的一个因素,或者只是一个红鲱鱼。

现在对于一些原始错误的最后花絮(它们来自正在生成的 TemplateSyntaxError 页面 - 最后 5 个)。

# /var/project/src/lib/sorl/thumbnail/base.py in get_thumbnail

  36. thumbnail = ImageFile(name, default.storage)
  37. cached = default.kvstore.get(thumbnail)
  38. if cached:
  39. return cached
  40. if not thumbnail.exists():
  41. # We have to check exists() because the Storage backend does not
  42. # overwrite in some implementations.
  43. source_image = default.engine.get_image(source) ...
  44. # We might as well set the size since we have the image in memory
  45. size = default.engine.get_image_size(source_image)
  46. source.set_size(size)
  47. self._create_thumbnail(source_image, geometry_string, options,
  48. thumbnail)
  49. # If the thumbnail exists we don't create it, the other option is

▼ Local vars Variable Value cached None file_ <EventImage: TestImage> geometry_string u'200x100' key 'format' name 'cache/24/69/246986cc89951f1d37235b8f0dec0a54.jpg' options {'colorspace': 'RGB', 'crop': False, 'format': 'JPEG', 'quality': 95, 'upscale': True} self    <sorl.thumbnail.base.ThumbnailBackend object at 0x7f5b885f2c10> source <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0> thumbnail <sorl.thumbnail.images.ImageFile object at 0x7f5b8867f510> value 'JPEG'
# /var/project/src/lib/sorl/thumbnail/engines/pil_engine.py in get_image

   5. from PIL import Image, ImageDraw
   6. except ImportError:
   7. import Image, ImageDraw
   8.
   9.
  10. class Engine(EngineBase):
  11. def get_image(self, source):

  12. buf = StringIO(source.read()) ...

  13. return Image.open(buf)
  14.
  15. def get_image_size(self, image):
  16. return image.size
  17.
  18. def dummy_image(self, width, height):

▼ Local vars Variable Value self <sorl.thumbnail.engines.pil_engine.Engine object at 0x7f5b886cf450> source <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0>
# /home/caronc/Development/cityattention/src/lib/sorl/thumbnail/images.py in read

 114. return self._size
 115.
 116. @property
 117. def url(self):
 118. return self.storage.url(self.name)
 119.
 120. def read(self):
 121. return self.storage.open(self.name).read() ...
 122.
 123. def write(self, content):
 124. if not isinstance(content, File):
 125. content = ContentFile(content)
 126. self._size = None
 127. return self.storage.save(self.name, content)

▼ Local vars Variable Value self <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0>
# /usr/lib/python2.6/site-packages/django/core/files/storage.py in open

  25. # These shouldn't be overridden by subclasses unless absolutely necessary.
  26.
  27. def open(self, name, mode='rb', mixin=None):
  28. """
  29. Retrieves the specified file from storage, using the optional mixin
  30. class to customize what features are available on the File returned.
  31. """
  32. file = self._open(name, mode) ...
  33. if mixin:
  34. # Add the mixin as a parent class of the File returned from storage.
  35. file.__class__ = type(mixin.__name__, (mixin, file.__class__), {})
  36. return file
  37.
  38. def save(self, name, content):

▼ Local vars Variable Value mixin None mode 'rb' name u'TestImage' self      <django.core.files.storage.FileSystemStorage object at 0x7f5b8867f3d0>
# /usr/lib/python2.6/site-packages/django/core/files/storage.py in _open

 130. location = settings.MEDIA_ROOT
 131. if base_url is None:
 132. base_url = settings.MEDIA_URL
 133. self.location = os.path.abspath(location)
 134. self.base_url = base_url
 135.
 136. def _open(self, name, mode='rb'):
 137. return File(open(self.path(name), mode)) ...
 138.
 139. def _save(self, name, content):
 140. full_path = self.path(name)
 141.
 142. directory = os.path.dirname(full_path)
 143. if not os.path.exists(directory):

▼ Local vars Variable Value mode 'rb' name u'TestImage' self <django.core.files.storage.FileSystemStorage object at 0x7f5b8867f3d0>

我希望我的问题只是一个快速问题,因为除了模板显示之外,一切都正常。

I've set up django-sorl and I truly think I'm overlooking something simple. I keep getting the following error (TEMPLATE_DEBUG = True and THUMBNAIL_DEBUG = True)

Request Method: GET Request URL:
http://localhost:8000/events/edit/1

Django Version: 1.2.4 Exception Type:
TemplateSyntaxError Exception Value:

Caught IOError while rendering: (2, 'No such file or directory')

Exception Location:
/usr/lib/python2.6/site-packages/django/core/files/storage.py in _open, line 137

Python Executable:
/usr/bin/python Python Version:
2.6.4

I'm running a redis server setup... my sorl config is:

settings.py -snip-:

TEMPLATE_DEBUG = DEBUG
THUMBNAIL_DEBUG = DEBUG
THUMBNAIL_KVSTORE = 'sorl.thumbnail.kvstores.redis_kvstore.KVStore'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'sorl.thumbnail',
)

models.py -snip-:

from sorl.thumbnail import ImageField
...
    path = ImageField(
       _('Image'),
        upload_to='event/%Y/%m/%d/%H',
    )
...

template.html -snip-:

{% load thumbnail %}
...
{% thumbnail image "200x100" as im %}
    <img src="{{ im.url }}" alt="{{ im.name }}"/>
{% endthumbnail %}
...

I figure I'm close because files are being created on demand in the cache folder

sh> find static/cache/ -type f 
static/cache/db/ac/dbac605942d9651eb25f16cf05f5e672.jpg
static/cache/82/bc/82bcdd2ab079187c6b6110cb0e0c1505.jpg
static/cache/07/77/077784411739d4f8b1758255783388ec.jpg

When I open these files; they are the images I'm requesting on-demand. It seems to be bombing while trying to open the image to display it. Another thing to note is that if I just display the image using the following syntax it will work fine (without sorl):

working template.html (without sorl - hence... no thumbnails) -snip-:

<img src="{{ MEDIA_URL}}{{ image.path }}" alt="{{ image.name }}"/>

The key being; that I do need to prefix the {{ MEDIA_URL }} tag for the image to be present (I'm not sure if this is a factor in the error I'm getting with sorl, or just a red-herring.

Now for some final tidbits of the original error (They are from the TemplateSyntaxError page being generated - last 5).

# /var/project/src/lib/sorl/thumbnail/base.py in get_thumbnail

  36. thumbnail = ImageFile(name, default.storage)
  37. cached = default.kvstore.get(thumbnail)
  38. if cached:
  39. return cached
  40. if not thumbnail.exists():
  41. # We have to check exists() because the Storage backend does not
  42. # overwrite in some implementations.
  43. source_image = default.engine.get_image(source) ...
  44. # We might as well set the size since we have the image in memory
  45. size = default.engine.get_image_size(source_image)
  46. source.set_size(size)
  47. self._create_thumbnail(source_image, geometry_string, options,
  48. thumbnail)
  49. # If the thumbnail exists we don't create it, the other option is

▼ Local vars Variable Value cached None file_ <EventImage: TestImage> geometry_string u'200x100' key 'format' name 'cache/24/69/246986cc89951f1d37235b8f0dec0a54.jpg' options {'colorspace': 'RGB', 'crop': False, 'format': 'JPEG', 'quality': 95, 'upscale': True} self    <sorl.thumbnail.base.ThumbnailBackend object at 0x7f5b885f2c10> source <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0> thumbnail <sorl.thumbnail.images.ImageFile object at 0x7f5b8867f510> value 'JPEG'
# /var/project/src/lib/sorl/thumbnail/engines/pil_engine.py in get_image

   5. from PIL import Image, ImageDraw
   6. except ImportError:
   7. import Image, ImageDraw
   8.
   9.
  10. class Engine(EngineBase):
  11. def get_image(self, source):

  12. buf = StringIO(source.read()) ...

  13. return Image.open(buf)
  14.
  15. def get_image_size(self, image):
  16. return image.size
  17.
  18. def dummy_image(self, width, height):

▼ Local vars Variable Value self <sorl.thumbnail.engines.pil_engine.Engine object at 0x7f5b886cf450> source <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0>
# /home/caronc/Development/cityattention/src/lib/sorl/thumbnail/images.py in read

 114. return self._size
 115.
 116. @property
 117. def url(self):
 118. return self.storage.url(self.name)
 119.
 120. def read(self):
 121. return self.storage.open(self.name).read() ...
 122.
 123. def write(self, content):
 124. if not isinstance(content, File):
 125. content = ContentFile(content)
 126. self._size = None
 127. return self.storage.save(self.name, content)

▼ Local vars Variable Value self <sorl.thumbnail.images.ImageFile object at 0x7f5b885c81d0>
# /usr/lib/python2.6/site-packages/django/core/files/storage.py in open

  25. # These shouldn't be overridden by subclasses unless absolutely necessary.
  26.
  27. def open(self, name, mode='rb', mixin=None):
  28. """
  29. Retrieves the specified file from storage, using the optional mixin
  30. class to customize what features are available on the File returned.
  31. """
  32. file = self._open(name, mode) ...
  33. if mixin:
  34. # Add the mixin as a parent class of the File returned from storage.
  35. file.__class__ = type(mixin.__name__, (mixin, file.__class__), {})
  36. return file
  37.
  38. def save(self, name, content):

▼ Local vars Variable Value mixin None mode 'rb' name u'TestImage' self      <django.core.files.storage.FileSystemStorage object at 0x7f5b8867f3d0>
# /usr/lib/python2.6/site-packages/django/core/files/storage.py in _open

 130. location = settings.MEDIA_ROOT
 131. if base_url is None:
 132. base_url = settings.MEDIA_URL
 133. self.location = os.path.abspath(location)
 134. self.base_url = base_url
 135.
 136. def _open(self, name, mode='rb'):
 137. return File(open(self.path(name), mode)) ...
 138.
 139. def _save(self, name, content):
 140. full_path = self.path(name)
 141.
 142. directory = os.path.dirname(full_path)
 143. if not os.path.exists(directory):

▼ Local vars Variable Value mode 'rb' name u'TestImage' self <django.core.files.storage.FileSystemStorage object at 0x7f5b8867f3d0>

I unsuccessfully Googled for this error; I'm hoping that my problem is just a quick one since everything is working except the template display.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-10-27 21:43:45

文档在这方面有点薄弱,但是在导入python调试器之后,我能够找出问题出在哪里。

此问题有 2 个可能的解决方案。为我解决所有问题的第一个是将我的模板更改为:

{% load thumbnail %}
...
{% thumbnail image.path "200x100" as im %}
    <img src="{{ im.url }}" alt="{{ im.name }}"/>
{% endthumbnail %}
...

我猜这个问题可能仍然在我的最后;特别是因为这个工具已经防弹了一段时间了。也就是说,那些遇到同样问题的人需要添加 .path 参数来避免我所经历的悲伤。

另一种(不是很好)的选择是将其添加到您的 models.py 中,然后再次直接引用图像(就像我在最初的问题中所做的那样)。

def __unicode__(self):
    return self.image.path

如果包含您尝试显示的事件的对象不仅仅负责显示图像,这当然是一个令人讨厌的解决方案。因此......第二种选择可能只在“某些”情况下才是理想的。我会坚持第一个解决方法。

此解决方法适用于 10.12.1 补丁。

另外,为了证明我最初的问题是合理的。我说过正在创建静态图像;事实证明,这是错误的。尽管这些图像“是”图像的正确缩略图,但我无法在我的页面上工作。它们是从 DJango-Admin 工具自动生成的。可能是当我尝试上传不同的图像时(以错误的方式解决问题)。 :)

The documentation was a bit weak in this field, but after importing the python debugger, I was able to find out where the problem was.

There are 2 potential solutions to this issue. The first one that fixed everything for me was to alter my template to read:

{% load thumbnail %}
...
{% thumbnail image.path "200x100" as im %}
    <img src="{{ im.url }}" alt="{{ im.name }}"/>
{% endthumbnail %}
...

I'm guessing the issue still may be at my end; especially since this tool has been bulletproof for some time now. That said, those who are experiencing the same issue will need to add the .path argument to save themselves the grief I went through.

An alternative (not a very good one) option is to add this to your models.py and then reference the image directly again (as I did in my initial question).

def __unicode__(self):
    return self.image.path

This of course is a nasty solution if the object containing the event your trying to display is not solely responsible for displaying images. Hence... the second option may only be ideal in 'some' situations. I'd stick with the first workaround.

This work-around is applicable to the 10.12.1 patch.

Also, to justify my initial question. I stated that static images were being created; as it turns out, this was incorrect. Although those images 'were' the correct thumbnails of the images I couldn't get to work on my page. They were being automatically generated from the DJango-Admin tool. Probably back when I was trying to upload different images (tackling the problem the wrong way). :)

三岁铭 2024-10-27 21:43:45

在您的模型中,您有一个属性path。此路径被定义为ImageField,最终这是访问图像以制作最终缩略图的位置。 sorl.thumbnail 无法通过传递模型实例来猜测您的意思,您需要传递图像 source 在您的情况下似乎是 image.path 。您关于在 __unicode__ 方法中返回 self.image.path 的评论似乎是错误的,如果您仍在谈论的话,可能应该是 self.path定义路径的模型相同。

In your model you have an attribute path. This path is defined as an ImageField and ultimately this is where the image will be accessed from to make the final thumbnail. There is no way that sorl.thumbnail will guess what you mean by passing the model instance, you need to pass the image source which seems to be image.path in your case. Your comment about returning self.image.path in the __unicode__ method seems wrong and probably should be self.path if you are still talking about the same model that defines path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文