【问题标题】:Unable to get images in list in Django views.py context, Throws List AttributeError无法在 Django views.py 上下文中获取列表中的图像,引发 List AttributeError
【发布时间】:2021-08-19 06:30:14
【问题描述】:

我正在使用下面的代码来获取所有图像的列表,然后显示它:

def index(request):
    data = cos.list_objects(Bucket='pixmedia')
    di = data['Contents']
    endpoint="https://s3.us.cloud-object-storage.XXXXX.cloud/XXX/"
    #print(di)
    image_list=[]
    for key in di:
        print("Key is--->",key['Key'])
        res=key['Key']
        res=endpoint + res
        print("Res is ---->",res)

        #context = {
        #    'image': res,
        #}
        image_list=image_list.append(res)
        print("Image List is",image_list)



    context = {
            {'image_list': image_list,}
    }

    return render(request, "index.html", context)

但是,我在启动 127.0.0.1:8000 时遇到错误:

image_list=image_list.append(res) AttributeError:“NoneType”对象没有“附加”属性。请帮忙。

【问题讨论】:

    标签: python python-3.x django list webserver


    【解决方案1】:

    image_list=image_list.append(res) 更改为image_list.append(res)。您正在重新分配 image_list 但 append 方法没有返回值,因此出现 NoneType 错误。列表是可变的,因此它会就地修改列表。

    【讨论】:

    • 我已经尝试过了 - image_list.append(res)。现在,我收到 TypeError: unhashable type: 'dict' 。我猜这是来自上下文。
    • 是的,你的上下文应该只有一组花括号
    • 现在,我得到了 image_list 中的所有图像 URL,但浏览器上没有显示任何内容。
    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2017-05-08
    相关资源
    最近更新 更多