我有一个如下所示的表单:
<form method="POST" action="/posts">
{{ csrf_field }}
<input type="text" name="username">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
但是当我提交此表单并尝试上传时,我只得到图像的名称:
def posts(self, request: Request, upload: Upload):
upload.store(request().input('image'))
我遇到了异常:
AttributeError >“str”对象没有属性“filename”
最佳答案
抛出此错误是因为您没有在此处的 HTML 表单上设置编码:
<form method="POST" action="/posts">
这应该更改为:
<form method="POST" action="/posts" enctype="multipart/form-data">
这将对图像进行编码,以便 Masonite 可以将其作为对象而不是字符串读取。
关于python - Masonite - AttributeError > 'str' 对象没有属性 'filename',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686542/