0

首先,一个 django 新手,所以放轻松;)

我正在尝试在 for 循环中做一些缩略图 - 下一件事将是分页或 group_by,但一次一个问题;)

问题是我有这个:

 {% for item in object_list %}
  <li>{{ item.name }}</a></li>
  {% endfor %}

并且还设法使用以下方法在views.py / item模板中使用sorl-thumbnail:(仅适用于单个项目)

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,
    })

所以我被困在 for/sorl-thumbnail 部分。我得到了这个,但是当 get() 返回多个结果时它不起作用:

{% for item in all_items %}
    <li>{{ item.name }}</li>
    {% if item.images.get %} 
      {{item.images.get }}
    {% endif %}
{% endfor %}
4

1 回答 1

2

我不是 100% 确定,但你的问题是,但如果你有一个图像列表,你也可以索引模板中的第一个:

{% load thumbail %}
{% if item.images.all %}
    <img src="{% thumbnail item.images.all.0 100x100 %}">
{% endif %}
于 2010-11-21T19:32:22.810 回答