函数没有在Python中调用,为什么?我该如何解决?

时间:2009-11-23 16:16:48

标签: python django python-imaging-library

我目前正在使用python / django网站,目前我的模板看起来像这样

 {% extends "shopbase.html" %}
{% block pageid %}products{% endblock %}
{% block right-content %}

<img src="{{MEDIA_URL}}/local/images/assets/products.png" alt="Neal and Wolf News" class="position"/>
    <div class="products">
    <form method="post" action="{% url category category.slug %}">
    {% for product in category.products.all %}
        <div class="{% cycle 'clear' '' '' %}">
            <img src="{{MEDIA_URL}}{{download.mini.thumbnail}}" alt="{{product.name}}" class="thumbnail"/>
            <h3><a href="{% url shop.views.product category.slug product.slug %}">{{ product.product_type_name }}</a></h3>
            <p class="strap">{{ product.product_sub_name }}</p>
            <p>{{ product.strap }}</p>
            <ul class="clear">
                <li class="price"><b>&pound;{{product.price}}</b></li>
                <li class="quantity">
                    <select name="quantity_{{product.id}}">
                        <option label="1" value="1">1</option>
                        <option label="2" value="2">2</option>
                        <option label="3" value="3">3</option>
                        <option label="4" value="4">4</option>
                        <option label="5" value="5">5</option>
                        <option label="6" value="6">6</option>
                        <option label="7" value="7">7</option>
                        <option label="8" value="8">8</option>
                        <option label="9" value="9">9</option>
                    </select>
                </li>
                <li><b><a href="details">Details &gt;</a></b></li>
                <li><input type="submit" name="add_to_basket_{{product.id}}" value="Add to Basket &gt;"/></a></li>
            </ul>
        </div>
    {% endfor %}
    </form>
    </div>
{% endblock %}

在模型中我有一个类似于

的类
def get_thumbnail(self, dimension, on=RES_X, use=USE_BOTH):
    """
    Generate a thumbnail image for this Store Object. The thumbnail will be
    of size 'dimension' on the axis specified by the on parameter (which is
    deduced from calling x_or_y). If the use parameter is set to USE_BOTH
    then the thumbnail will take priority in the generation order. In other
    words, the function checks for the existance of an uploaded thumbnail.
    If one exists, it will use that, otherwise the image field is used
    instead. Specifying either USE_IMAGE or USE_THUMBNAIL here will force
    the generation to a particular asset.
    The image is saved out as a jpeg with the quality set to 90. The
    path relative to the MEDIA_ROOT is then returned.
    """
    from PIL import Image
    import os

    if self.thumbnail and use != StoreObject.USE_IMAGE:
      source_path = str(self.thumbnail)
    elif self.image and use != StoreObject.USE_THUMBNAIL:
      source_path = str(self.image)
    else:
      return ""

    try:
      against = StoreObject.x_or_y(on)
    except KeyError:
      return ""

    target_path = os.path.join(StoreObject.GENERATED_THUMB_LOCATION, "%s-%d%s.png" % (self.slug, dimension, against))

    savepath = os.path.join(settings.MEDIA_ROOT, target_path)
    loadpath = os.path.join(settings.MEDIA_ROOT, source_path)

    if os.path.exists(savepath) and os.path.getmtime(savepath) > os.path.getmtime(loadpath):
      return target_path

    try:
      img = Image.open(loadpath)
    except IOError:
      return ""

    aspect = float(img.size[0]) / float(img.size[1])
    if against == StoreObject.RES_X:
      height = dimension / aspect
      width = dimension
    elif against == StoreObject.RES_Y:
      width = dimension * aspect
      height = dimension

    img = img.resize((width,height), Image.ANTIALIAS)

    img.convert('RGBA').save(savepath, "PNG")
    return target_path

def mini_thumbnail(self):
    """
    Generate and return the path to, a thumbnail that is 50px high
    """
    return self.get_thumbnail(50, "x")

def preview_image(self):
    """
    Generate and return the path to, a thumbnail that is 300px wide, build
    from the image field
    """
    return self.get_thumbnail(300, use=StoreObject.USE_IMAGE)

def preview_thumbnail(self):
    """
    Generate and return the path to, a thumbnail that is 300px wide, build
    from the thumbnail field
    """
    return self.get_thumbnail(300, use=StoreObject.USE_THUMBNAIL)

现在您可以看到我正在尝试从数据库中提取某个项目的某些信息,并在缩略图中显示该项目的图像,该缩略图由上面的缩略图功能生成(在我使用product.mini_thumbnail的模板中)然而,似乎知道图像?但是如果我用我的MEDIA_URL打印product.image,那么我会得到完整的图像。任何人都可以建议可能发生的事情吗?

2 个答案:

答案 0 :(得分:5)

与Django中的许多内容一样......有人已经解决了这个问题,并提出了一个干净,灵活的解决方案来生成/存储不同大小的缩略图等。我强烈建议查看sorl-thumbnail项目。

文档:http://thumbnail.sorl.net/docs/

在此处下载:http://code.google.com/p/sorl-thumbnail/downloads/list

答案 1 :(得分:2)

您的模板有{{download.mini.thumbnail}}而不是{{download.mini_thumbnail}}