sorl-thumbnail:在模板的 for 循环内创建缩略图
sorl-thumbnail: Creating a thumbnail inside a for loop in a template
我正在尝试从图像创建大小为 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 错误。
看来你的复数用错了:
{% for post_images in post.sellpostimage_set.all %}
应该是:
{% for post_image in post.sellpostimage_set.all %}
post_image = SellPostImage.objects.filter(post=poster_posts)
context_dict['post_image'] = post_image
应该是:
post_images = SellPostImage.objects.filter(post=poster_posts)
context_dict['post_images'] = post_images
或:
post_image = SellPostImage.objects.get(post=poster_posts)
context_dict['post_image'] = post_image
为什么在模板中这样做:
{% for post_images in post.sellpostimage_set.all %}
如果上下文中有 post_images
?
这是如何工作的?
<img class="thumbnail" src="{{ post_images.pictures.url }}" />
不应该是:
<img class="thumbnail" src="{{ post_image.picture.url }}" />
我正在尝试从图像创建大小为 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 错误。
看来你的复数用错了:
{% for post_images in post.sellpostimage_set.all %}
应该是:
{% for post_image in post.sellpostimage_set.all %}
post_image = SellPostImage.objects.filter(post=poster_posts)
context_dict['post_image'] = post_image
应该是:
post_images = SellPostImage.objects.filter(post=poster_posts)
context_dict['post_images'] = post_images
或:
post_image = SellPostImage.objects.get(post=poster_posts)
context_dict['post_image'] = post_image
为什么在模板中这样做:
{% for post_images in post.sellpostimage_set.all %}
如果上下文中有 post_images
?
这是如何工作的?
<img class="thumbnail" src="{{ post_images.pictures.url }}" />
不应该是:
<img class="thumbnail" src="{{ post_image.picture.url }}" />