gpt4 book ai didi

python - Django 错误 : io. UnsupportedOperation: read and ValueError: bad mode 'rb'

转载 作者:太空宇宙 更新时间:2023-11-04 00:52:32 24 4
gpt4 key购买 nike

我收到这个错误:io.UnsupoortedOperation: read我做了一些改变。我将此行 img = Image.open(f) 更改为 img = Image.open(f, "rb") 之后我收到此错误:ValueError : 坏模式 'rb'这里有什么问题?我该如何解决这个问题?

View .py

class LinkCreateView(CreateView):
model = Link
form_class = LinkForm

def form_valid(self, form):
hash = str(uuid.uuid1())
with open("tmp_img_original_{}.png".format(hash), "wb") as f:
res = requests.get(form.instance.url, stream=True)
if not res.ok: raise Exception("URL'de dosya yok: 404")
for block in res.iter_content(1024): f.write(block)

img = Image.open(f, "rb")
width, height = img.size
img.thumbnail(get_size(width, height), Image.ANTIALIAS)
img.save()

djfile = File(f)

form.img.save("img_tn_{}.png".format(hash), djfile, save=True)
f.close()

f = form.save(commit=False)
f.rank_score = 0.0
f.submitter = self.request.user
f.save()

return super(CreateView, self).form_valid(form)

最佳答案

答案是以“wb+”模式打开文件

改变这个

with open("tmp_img_original_{}.png".format(hash), "wb") as f:

with open("tmp_img_original_{}.png".format(hash), "wb+") as f:

关于python - Django 错误 : io. UnsupportedOperation: read and ValueError: bad mode 'rb',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com