2013-06-20 47 views
1

为什么thumbnail模板标签不适用于下面详述的设置?Django solr-thumbnail redis配置

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缓存中的密钥,则应该创建新请求的映像。实际上,在检查时,redis缓存根本没有任何项目:

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

因此,sorl-thumbnail甚至没有在redis上创建密钥。我无法弄清楚问题所在,因为我什么都不例外。我有感觉我错过了某个步骤。 (通过蛋黄)

相关的Python包版本:

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

回答

1

当模板不工作应该加上:

THUMBNAIL_DEBUG = True 

的设置文件,然后有用例外,为什么将会生成thumbnail模板标签不工作。 (强烈建议禁用此设置生产。)

然后我不得不做2个修复得到上述工作:

第一:改变

THUMBNAIL_REDIS_PORT = '6379' 

整数:

THUMBNAIL_REDIS_PORT = 6379 

二:改变图像参数的thumbnail标签是图片的网址,而不是简单地ŧ他的图像和裁切模式为“中心”,而不是“聪明”,即我改变:

{% 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;如果您对此有任何建议,请添加答案/评论。谢谢。