sorl缩略图未加载图像

时间:2015-11-27 02:39:43

标签: python django sorl-thumbnail

我无法使用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,
        }),
    ]
...

我被困在这一段时间了。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

检查以下内容:

  • {{ article.image.url }}输出任何内容吗?
  • {{ im.url }}输出任何内容吗?
  • 是您加载页面时创建的任何thumnail?检查媒体文件夹。
  • 您确定THUMBNAIL_DEBUGTrue吗?
    • ./ manage.py shell
    • 来自django.conf导入设置
    • print settings.THUMBNAIL_DEBUG
  • pip freeze =&gt;仔细检查您正在使用的版本的文档

此外,您还有一个可选图片:

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

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

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

答案 1 :(得分:0)

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

替换:

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

使用:

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

我的另一个答案保持不变。您需要检查是否有图像:

{% if upload.image %}