【问题标题】:Django solr-thumbnail redis configurationDjango solr-thumbnail redis 配置
【发布时间】:2013-06-17 06:46:35
【问题描述】:

为什么thumbnail 模板标签不适用于如下详述的设置?

Django项目是这样配置的(相关文件的摘录):

settings.py(摘录)

INSTALLED_APPS = (
    ..
    'sorl.thumbnail',
    ..
)

..

# solr-thumbnail related settings
THUMBNAIL_FORMAT = 'PNG'
THUMBNAIL_KVSTORE = 'sorl.thumbnail.kvstores.redis_kvstore.KVStore'
THUMBNAIL_REDIS_HOST = 'localhost' # default
THUMBNAIL_REDIS_PORT = '6379' # default

models.py

from django.db import models
from sorl.thumbnail import ImageField

class UserProfile(models.Model):
    email = models.EmailField()
    profile_pic = ImageField(upload_to='user_profile_imgs')

views.py(摘录)

form = NewUserForm(request.POST, request.FILES)
if form.is_valid():
    user_inst = UserProfile(
        email=form.cleaned_data['email'],
        profile_pic=request.FILES['image']
    )
    user_inst.save()
    return ..

HTML 模板(摘录)

<p>Actual image</p> <!-- THIS WORKS -->
<img src="{{ user.profile_pic.url }}" alt="user profile pic"> 

<p>Cropped image</p> <!-- THIS DOES NOT WORK -->
{% thumbnail user.profile_pic "100x100" crop="smart" as im %}
    <img src="{{ im.url }}" alt="user profile pic 100 by 100">
{% endthumbnail %}

裁剪后的图像不起作用;根据sorl-django workflow,如果在我配置的 Redis-cache 中找不到密钥,则应创建新请求的图像。事实上,当检查 redis 缓存时根本没有任何项目:

redis 127.0.0.1:6379> KEYS *
(empty list or set)

所以 sorl-thumbnail 甚至没有在 redis 上创建密钥。我无法弄清楚问题是什么,因为我没有任何例外。我觉得我在某个地方错过了一步。

相关的 Python 包版本(通过 yolk):

Django          - 1.5.1        - active
Pillow          - 2.0.0        - active
redis           - 2.7.6        - active
sorl-thumbnail  - 11.12        - active

【问题讨论】:

    标签: django redis sorl-thumbnail


    【解决方案1】:

    当模板不起作用时,应添加:

    THUMBNAIL_DEBUG = True
    

    到设置文件,然后有用将产生thumbnail模板标签为什么不起作用的异常。 (强烈建议在生产环境中禁用此设置。)

    然后我必须进行 2 次修复才能使上述工作正常进行:

    首先:改变

    THUMBNAIL_REDIS_PORT = '6379'
    

    转为整数:

    THUMBNAIL_REDIS_PORT = 6379
    

    第二:将图像参数更改为thumbnail标签作为图像的URL,而不是简单的图像和裁剪模式为“中心”而不是“智能”,即我改变了:

    {% thumbnail user.profile_pic "100x100" crop="smart" as im %}
        <img src="{{ im.url }}" alt="user profile pic 100 by 100">
    {% endthumbnail %}
    

    到:

    {% thumbnail user.profile_pic.url "100x100" crop="center" as im %}
        <img src="{{ im.url }}" alt="user profile pic 100 by 100">
    {% endthumbnail %}
    

    做到了!我仍然想使用smart cropping;如果您对此有任何想法,请添加答案/评论。谢谢。

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 2015-04-05
      • 2015-07-21
      • 2011-07-26
      相关资源
      最近更新 更多