本文介绍了Django sorl-thumbnail不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Django sorl-thumbnail,但不显示图像(并没有显示任何错误)。

I try to use Django sorl-thumbnail but it does not display images (and does't show any errors).

Settings.py:

Settings.py:

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

型号:

class Toy(models.Model):
    name = models.CharField(max_length=50, verbose_name=u'Name')
    image = ImageField(upload_to='site_media/images')

模板:

<div id="toy">
                {% for p in toys %}
                    <div class="toy">
                                # toy.image - this is model_name.image field
                            {% thumbnail toy.image "100x700" as im %}
                            <img style="margin:{{ im|margin:"100x700" }}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}">
                            {% endthumbnail %}

                            <p>
                            <span> {{ p.name }} </span>
                            <span> {{p.unit_price}} </span>
                            </p>
                    </div>
                {% endfor %}
            </div>

urls.py:

url(r'^site_media/(.*)$', 'django.views.static.serve', {'document_root' : os.path.join(os.path.dirname(__file__), 'site_media')}),


推荐答案

ImageField?有必要让它自动工作。

Have you imported ImageField from sorl in model definition? It's necessary to get it working automatically.

此外 ./ manage.py syncdb ,因为在默认配置中,sorl在数据库中保留了缩略图的缓存名称。它必须在数据库中创建自己的表。

Moreover you need to run in console ./manage.py syncdb because in default configuration sorl keeps cached names of thumbnails in database. It has to create its own table in database for that.

您还可以显示您的STATIC_URL,STATICFILES_DIRS等设置吗?

Could you also show your settings of STATIC_URL, STATICFILES_DIRS, etc?

这篇关于Django sorl-thumbnail不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:57