我正在尝试从数据库中获取多个图像路径以显示它们,但是当前不起作用。

这是我正在使用的:


def get_image(self, userid, id):
    image = meta.Session.query(Image).filter_by(userid=userid)
    permanent_file = open(image[id].image_path, 'rb')
    if not os.path.exists(image.image_path):
        return 'No such file'
    data = permanent_file.read()
    permanent_file.close()
    response.content_type = guess_type(image.image_path)[0] or 'text/plain'
    return data



我在此部分出现错误:


image[id].image_path



我想要的是Pylons在一页上显示几个jpg文件。任何想法我怎么能做到这一点?

最佳答案

您两次使用image.image_path,但是在一处(您告诉我们,遇到错误)中,您使用了image[id].image_path。您认为什么id可能是image的正确索引,为什么在代码的不同位置之间使用差异?

如果需要一定数量的图像,为什么不使用切片语法?例如。您可以获取前10张图像(请确保包含order_by以获得可预测的,可重复的结果),然后可以将filter_by的结果按[0:10]进行切片;后10张图片,[10:20];等等。

关于python - 如何显示多张图片?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2841917/

10-12 22:20
查看更多