Django: sorl-thumbnail in a for template
First of all, a django newbie, so take it easy ;)
I'm trying to do some thumbnails within a for loop - the next thing will be Pagination, or group_by, but one problem at a time ;)
The thing is I've this:
{% for item in object_list %}
<li>{{ item.name }}</a></li>
{% endfor %}
And also managed to work the sorl-thumbnail in views.py / item template using this: (only works for a single item)
def get_item(request, item_slug):
item = get_object_or_404(Item, slug_name=item_slug)
# get() returned more than one
# img = item.images.get()
imgs = item.images.filter(is_poster=True)
img_src = imgs[0].src if imgs else None
return render_to_response('items/get_item.html', {
'item': item,
'title': item.name,
'image': img_src,
})
So I'm stucked in the for/sorl-thumbnail part. I got this but it isn't working when get() returns more than one result:
{% for 开发者_如何转开发item in all_items %}
<li>{{ item.name }}</li>
{% if item.images.get %}
{{item.images.get }}
{% endif %}
{% endfor %}
I'm not 100% sure but your problem is, but if you have a list of images you can also index the first in the template:
{% load thumbail %}
{% if item.images.all %}
<img src="{% thumbnail item.images.all.0 100x100 %}">
{% endif %}
精彩评论