【问题标题】:sorl thumbnails not loading imagessorl 缩略图不加载图像
【发布时间】:2016-03-01 04:38:58
【问题描述】:

我无法使用 sorl.thumbnails 获取缩略图。 我没有收到任何错误。

我做了什么:

settings.py

INSTALLED_APPS = {
...
'sorl.thumbnail',
}  

THUMBNAIL_DEBUG = True
...

models.py

...
from sorl.thumbnail import ImageField
...
class Upload(models.Model):
    article = models.ForeignKey(Article, related_name="uploads")
    image = models.ImageField(upload_to=get_image_path, blank=True)

模板

{% extends 'layouts/base.html' %}
{% block title %}Home - {{ block.super }}{% endblock %}
{% load staticfiles %}
{% load thumbnail %}

{% block content %}
<section class="main clearfix">
{% for article in articles %}
  <div class="work">

    <a href="{% url 'article_detail' slug=article.slug %}">
      {% for upload in article.uploads.all %}
      {% thumbnail article.image "200x200" as im %}
        <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"class="media" alt=""/>
      {% endthumbnail %}
      {% endfor %}

    <div class="caption">
      <div class="work_title">
        <h1>{{ article.name }}</h1>
      </div>
    </div>
    </a>
  </div>

{% endfor %}
</section><!-- end main -->
{% endblock %}

views.py

...
def index(request):
    articles = Article.objects.all().order_by('-featured')
    return render(request, 'index.html', {
        'articles': articles,
    })

def article_detail(request, slug):
    #grab the object
    article = Article.objects.get(slug=slug)

    #grab all object Images
    uploads = article.uploads.all()
    # pass to the template
    return render(request, 'articles/article_detail.html', {
        'article': article,
        'uploads': uploads,
    })
...

urls.py

...
if settings.DEBUG:
    urlpatterns += [
    url(r'^media/(?P<path>.*)$','django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT,
        }),
    ]
...

我已经坚持了一段时间。任何帮助将不胜感激。

【问题讨论】:

    标签: python django sorl-thumbnail


    【解决方案1】:

    检查以下内容:

    • {{ article.image.url }} 有输出吗?
    • {{ im.url }} 有输出吗?
    • 加载页面时是否创建了任何缩略图?检查您的媒体文件夹。
    • 你确定THUMBNAIL_DEBUGTrue 吗?
      • ./manage.py 外壳
      • 从 django.conf 导入设置
      • 打印设置.THUMBNAIL_DEBUG
    • pip freeze => 仔细检查文档以了解您正在使用的版本

    另外,您还有一张可选图片:

    image = models.ImageField(upload_to=get_image_path, blank=True)
    

    所以你可能想检查是否有图像:

    {% if article.image %} 
       THE THUMBNAIL 
    {% else %} 
       A PLACEHOLDER?  
    {% endif %}
    

    【讨论】:

    • {{ article.image.url }} 有输出吗? - 不,甚至没有空白图像图标。 {{ im.url }} 有输出吗? - 不,甚至没有空白图像图标。不会在媒体中创建缩略图。 THUMBNAIL_DEBUG 为真 pilkit==1.1.13 Pillow==3.0.0 sorl-thumbnail==12.3 使用了模板中的条件仍然没有返回。还有其他想法吗?
    • 我编辑了这篇文章。缩略图应该是使用 article_detail 中的图像从索引生成的
    【解决方案2】:

    啊,图片在你的Upload 模型上!

    替换:

    {% thumbnail article.image "200x200" as im %}
    

    与:

    {% thumbnail upload.image "200x200" as im %}
    

    我的其他答案仍然正确。您需要检查是否有图像:

    {% if upload.image %}
    

    【讨论】:

    • 这是有道理的。这里仍然没有运气。我已将静态文件 url 添加到帖子中。我认为查找图像可能是个问题,因为 {% else %} 条件正在加载我的

      占位符。

    • 如果你正在加载占位符,这意味着upload 没有图像。与static路径无关,因为用户上传的文件在media路径下。
    猜你喜欢
    • 2013-11-11
    • 2017-08-19
    • 2012-09-18
    • 2017-05-07
    • 2010-11-27
    • 2011-07-01
    • 2013-04-29
    • 2012-03-06
    • 2015-06-10
    相关资源
    最近更新 更多