简体   繁体   English

Django:无法将上传的图像绑定到表单ImageField()

[英]Django: can't bind an uploaded image to a form ImageField()

here's my problem, I've created this form: 这是我的问题,我创建了这个表单:

class SettingsForm(forms.Form):  
    ...  
    logo = forms.ImageField()  
    ...

The upload works fine and I managed to display the image but I can't bind it to the form. 上传工作正常,我设法显示图像,但我无法将其绑定到表单。 Here's what I've done: 这就是我所做的:

data = ...  
files = {'logo': SimpleUploadedFile('logo.jpg', logo.read())}  
form = SettingsForm(data=data, files=files)  

the logo object is a ImageFieldFile . 徽标对象是ImageFieldFile I've tested the read method in a shell, it's ok. 我已经在shell中测试了read方法,没关系。 I've got no warnings displaying the page, only "no file chosen". 我没有显示页面的警告,只有“没有选择文件”。

Thanks for your help. 谢谢你的帮助。 Sorry for the format of this post, I'm new to stackoverflow and to django. 很抱歉这篇文章的格式,我是stackoverflow和django的新手。

我不确定这一点,但根据django文档,在绑定表单上,数据和文件不是kwargs,而是args,所以试试这个:

form = SettingsForm(data, files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM