【发布时间】:2017-06-10 16:27:31
【问题描述】:
我有这个文件处理功能:
def handle_uploaded_file(f, person):
with open(user_directory_path(person, f.name), 'w') as destination:
for chunk in f.chunks():
destination.write(chunk)
我在表单保存方法中调用了这个函数:
class CreateMessageForm(forms.Form):
files = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
...
def save(self):
...
for file in cleaned_data.get('files'):
handle_uploaded_file(file, self.person)
message.files.add(file)
但是在发送这个表单后我有一个 AttributeError: 'bytes' object has no attribute 'name'。我不明白出了什么问题。在文档中上传的文件对象有这个属性。
【问题讨论】:
-
在您的 html 表单中添加
enctype="multipart/form-data" -
@itzmeontv 已经添加了。对不起,我可能会写一下。
标签: django forms file python-3.x