如何在表单向导中的 clean 方法之前保存提交的表单图像字段(Django 1.2)
在表单向导中提交表单时,我收到图像字段的“此字段为必填”错误。
class SignupForm(forms.ModelForm):
username = forms.CharField(
label = _("Username*"),
max_length = 30,
widget = forms.TextInput()
)
image = forms.ImageField(label = _("Profile picture*"), required=True)
class Meta:
model = Profile
fields = ('name','gender','birth_date',)
这是我在表单向导中的最后一个表单。提交此表单后,我在 clean 方法中获得了用户名和其他字段,但没有获得图像。有没有办法在验证此表单之前保存图像以避免“此字段为必填”错误?
I get "This field is required" error for image field on submitting a form in Form Wizard.
class SignupForm(forms.ModelForm):
username = forms.CharField(
label = _("Username*"),
max_length = 30,
widget = forms.TextInput()
)
image = forms.ImageField(label = _("Profile picture*"), required=True)
class Meta:
model = Profile
fields = ('name','gender','birth_date',)
This is my last form in form wizard. On submitting this form I got username and other fields in clean method but not the image. Is there a way to save image before validation of this form to avoid "This field is required" error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要通过表单向导上传图像,您应该更改文件“django/contrib/formtools/wizard.py”。此票证中提到了这些更改 code.djangoproject.com/ticket/7439。如果您不想更改 djnago 代码,则只需将“formtools”文件夹复制到您的应用程序中,然后在其中进行更改,而不是使用:
使用:
同时设置模板表单的 enctype="multipart/form-data" 。
To upload your images through Form Wizard you should change the file "django/contrib/formtools/wizard.py". These changes are mentioned in this Ticket code.djangoproject.com/ticket/7439. If you dont want to change djnago code then simply copy "formtools" folder into your apps then make changes there and instead of using:
use:
Also set enctype="multipart/form-data" of your template form.