我无法使用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,
}),
]
...
我被困在这一段时间了。任何帮助将不胜感激。
答案 0 :(得分:0)
检查以下内容:
{{ article.image.url }}
输出任何内容吗?{{ im.url }}
输出任何内容吗?THUMBNAIL_DEBUG
是True
吗?
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 %}