【问题标题】:Django: sorl-thumbnail in a for templateDjango:模板中的 sorl-thumbnail
【发布时间】:2011-05-13 11:49:02
【问题描述】:

首先,一个 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 %}

【问题讨论】:

    标签: django django-templates django-views sorl-thumbnail


    【解决方案1】:

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

    {% load thumbail %}
    {% if item.images.all %}
        <img src="{% thumbnail item.images.all.0 100x100 %}">
    {% endif %}
    

    【讨论】:

    • 是的,虽然我必须看看如何在模板中执行 item.images.filter(is_poster=True),但目前这很好,除了应该是 item.images。 all.0.src
    • 您可以为您的图像创建一个自定义管理器 (djangoproject.com/documentation/models/custom_managers) 方法,例如。 is_poster,您也可以从模板 ({{ item.images.is_poster }}) 调用。
    猜你喜欢
    • 2016-05-09
    • 2013-08-26
    • 2011-09-12
    • 2012-06-21
    • 2012-11-22
    • 2013-01-30
    • 2013-06-20
    • 2012-04-21
    • 2012-09-05
    相关资源
    最近更新 更多