Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
属性错误:';非类型';对象没有属性';缩略图';:python django_Python_Django_Attributeerror - Fatal编程技术网

属性错误:';非类型';对象没有属性';缩略图';:python django

属性错误:';非类型';对象没有属性';缩略图';:python django,python,django,attributeerror,Python,Django,Attributeerror,我在这样的模板中渲染图像: {% if feature.photo %} {% render_photo feature.photo_inverted "Product large" class="img-responsive img-full" %} {% endif %} 其中,feature是产品模型中产品对象的实例。有时它会给人一种感觉 Attribute: 'NoneType' object has no attribute 'thumbnails': python-django

我在这样的模板中渲染图像:

{% if feature.photo %}
{% render_photo feature.photo_inverted "Product large" class="img-responsive img-full" %}
{% endif %}
其中,feature是产品模型中产品对象的实例。有时它会给人一种感觉

Attribute: 'NoneType' object has no attribute 'thumbnails': python-django
当我尝试加载模板时,有时会在渲染图像时成功加载模板。有没有办法解决这个问题

编辑 我使用的方法是一个模板标记,如下所示:

def render_photo(photo, size_name, *args, **kwargs):
    """Renders the HTML markup for the given photo.

    If the given size does not exist, the photo is displayed in its
    original size. If the size does exist, but there is no thumbnail of
    that size, the original image is used and displayed at the desired size.

    """
    html = '<img src="{0}{1}" alt="{2}" width="{3}" height="{4}" {5}/>'
    attrs = ''
    for key, value in kwargs.items():
        if len(attrs) > 0: attrs += ' '
        attrs += '{0}="{1}"'.format(key, value)
    try:
        thumbnail = photo.thumbnails.get(size__name=size_name)
        return html.format(project_settings.MEDIA_URL, thumbnail.image,
                           thumbnail.title, thumbnail.size.width,
                           thumbnail.size.height, attrs)
    except Photo.DoesNotExist: # There is no thumbnail of the given size
        try:
            size = Size.objects.get(name=size_name)
            width, height = size.width, size.height
        except Size.DoesNotExist: # The given size does not exist
            dimensions = settings.SIZES.get(size_name)
            if dimensions:
                # The size is defined in the settings
                width, height = dimensions
                Size.objects.create(name=size_name, width=width, height=height)
            else:
                width, height = photo.width, photo.height
        return html.format(project_settings.MEDIA_URL, photo.image, photo.title,
                           width, height, attrs)
def render_photo(照片、大小、名称、*args、**kwargs):
“”呈现给定照片的HTML标记。
如果给定的大小不存在,则照片将显示在其
原始大小。如果大小确实存在,但没有
在该大小下,将使用原始图像并以所需大小显示。
"""
html=“”
属性=“”
对于键,kwargs.items()中的值:
如果len(attrs)>0:attrs+=''
attrs+='{0}=“{1}”。格式(键,值)
尝试:
缩略图=照片.缩略图.获取(大小\名称=大小\名称)
返回html.format(project\u settings.MEDIA\u URL、thumbnail.image、,
缩略图.title,缩略图.size.width,
缩略图(大小、高度、属性)
除了Photo.DoesNotExist:#没有给定大小的缩略图
尝试:
size=size.objects.get(name=size\u name)
宽度,高度=size.width,size.height
除了Size.DoesNotExist:#给定的大小不存在
维度=设置。大小。获取(大小\名称)
如果尺寸:
#大小在“设置”中定义
宽度、高度=尺寸
Size.objects.create(name=Size\u name,width=width,height=height)
其他:
宽度,高度=photo.width,photo.height
返回html.format(project\u settings.MEDIA\u URL、photo.image、photo.title、,
宽度、高度、属性)

您确定这两种情况下都存在feature.photo吗?是的,我确定它们都存在。如果他们没有,它就不会通过If模板,图片有时渲染时不会出现任何错误,这就是为什么我如此困惑的原因。也许渲染部分更清晰。但是您在同一个pict上遇到了这个错误(比如once ok/once error),或者它可能与元数据中的渐进式jpeg/缩略图有关?发布执行此任务的函数,查看错误弹出的位置非常有用from@cox我已经编辑了这篇文章以包含我使用的方法。但是你的条件是如果feature.photo,但是render会反转feature.photo。为什么不在条件检查中反转feature.photo_?