简体   繁体   English

函数未在Python中调用,为什么? 以及我该如何解决?

[英]Function not being called in Python, why? and how can I solve it?

I am currently working on python/django site, at the moment I have a template that looks like this 我目前正在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 %}

and in the model I have a class that looks like this 在模型中,我有一个看起来像这样的课程

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)

Now as you can see I am trying to pull out of the database some information on a certain item, and show an image of that item in a thumbnail which generated from the thumbnail function above (in the template I using product.mini_thumbnail however there seems to be know image? however if I print product.image with my MEDIA_URL then I get the fullsized image. Can anyone suggest what might be happening? 现在您可以看到,我正在尝试从数据库中提取某些项目的信息,并在缩略图中显示该项目的图像,该缩略图是由上面的缩略图功能生成的(在我使用product.mini_thumbnail的模板中,知道图像吗?但是,如果我使用MEDIA_URL打印product.image,那么我会得到完整的图像。

As with many things in Django... someone has already solved this problem and come up with a clean, flexible solution for generating/storing thumbnails of different sizes etc. I'd strongly suggest looking at the sorl-thumbnail project. 与Django中的许多其他内容一样,某人已经解决了这个问题,并提出了一种干净,灵活的解决方案,用于生成/存储不同大小的缩略图等。我强烈建议您看一下sorl-thumbnail项目。

Docs here: http://thumbnail.sorl.net/docs/ 此处的文档: http : //thumbnail.sorl.net/docs/

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 function 是否在 Python 的 shell 中被调用? - How can you check if a function is being called In the shell in Python? 如何模拟任何未被直接调用的 function? - How can I mock any function which is not being called directly? 无法理解 function 是如何在 python 中被调用的 - Unable to understand how function is being called in python 在与Python绑定的OpenCV中,为什么不调用NumPy函数就可以使用它? - In OpenCV bound with Python, why the NumPy function can be used without it being called? 为什么要调用此函数(用于用户输入的python 切换函数)? - Why is this function being called (python switch function for user input)? 为什么在if语句中不调用该函数? (蟒蛇) - Why isn't the function being called in the if statement? (python) 不间断的睡眠导致我的Python程序真的很慢(如果是这样,我该如何解决这个问题?)? - Is uninterruptible sleep the cause of my Python program being really slow (and if so, how can I solve this?)? 如何在Python中传递被调用的函数值? - How can I pass on called function value in Python? 为什么这个函数被无限调用? - Why is this function being called infinitely? 如何将参数传递给Python vegas库中调用的函数? - How can I pass parameter to a function called in Python vegas library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM