【发布时间】:2015-06-14 20:24:11
【问题描述】:
我正在尝试从图像中创建大小为100x100 px 的缩略图。我正在使用 for 循环,for 循环有效并且图像被渲染,但由于某种原因,当我尝试创建缩略图时它不起作用。
这是我的代码:
{% for post_images in post.sellpostimage_set.all %}
<img class="thumbnail" src="{{ post_images.pictures.url }}" />
{% thumbnail post_images "100x100" crop="center" as im %}
<img src="{{ im.pictures.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
{% endfor %}
views.py
def post(request, post_name_slug):
context_dict = {}
try:
post = SellPost.objects.get(slug=post_name_slug)
context_dict['post'] = post
poster_posts = SellPost.objects.filter(user=post.user).order_by('-timestamp')
context_dict['poster_posts'] = poster_posts
post_image = SellPostImage.objects.filter(post=poster_posts)
context_dict['post_image'] = post_image
poster = post.user
context_dict['poster'] = poster
except SellPost.DoesNotExist:
return redirect('index')
return render(request, 'post.html', context_dict, )
编辑:如果我在缩略图 src {{ im.url }} 中使用,我可以获得奇怪的图像位置..
http://127.0.0.1:8000/media/cache/9b/0b/9b0b04d65b1075ed4c3e7783131caef6.jpg ,但缩略图仍然没有被渲染。显示为 URL 已损坏,当我尝试转到图像位置 url 时出现 404 错误。
【问题讨论】:
-
请显示
post_images定义 -
@SylvainBiehler 添加了。
标签: django sorl-thumbnail