我们在我们的网站中使用带有 Crispy 表单的 Django。我有一个由{% crispy form %}. 这是表单的代码:
class ProfileForm(AddAttributesToFieldsMixin, CleanDateOfBirthMixin, LocalizedFirstLastNameMixin, forms.ModelForm):
profile_picture = forms.ImageField(required=False, widget=CustomPhotoWidget, label=_('Update your profile picture'), error_messages={'required': _("A profile picture is required.")})
class Meta:
model = User
fields = ('slug', 'gender', 'date_of_birth', 'profile_picture')
使用的表单CustomPhotoWidget定义如下:
class CustomPhotoWidget(forms.widgets.Widget):
def render(self, name, value, attrs=None, renderer=None):
return render_to_string(template_name='accounts/edit_profile/widgets/photo_widget.html', context={
'name': name,
'user_photo': self.attrs['user'].photo,
})
但问题是,当我从浏览器上传文件时,我收到一条错误消息“没有提交文件。请检查表单上的编码类型。” 并且该文件未保存。表单的编码类型不正确。如何使用 Crispy 表单更改编码类型?
不负相思意
相关分类