使用 django-allauth 将 FileField 添加到自定义 SignupForm

Adding a FileField to a custom SignupForm with django-allauth

我有以下自定义 SignupForm(经过简化,无需 my_file 即可完美运行):

class SignupForm(forms.Form):
    home_phone = forms.CharField(validators=[phone_regex], max_length=15)
    my_file = forms.FileField()

    def signup(self, request, user):
        new_user = ReqInfo(
            user=user,
            home_phone=self.cleaned_data['home_phone'],
            my_file=my_file,
        )
        new_user.save()

在models.py中:

class ReqInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
    home_phone = models.CharField(
        validators=[phone_regex], max_length=15)
    my_file = models.FileField(upload_to='uploads/directory/', validators=[resume_file_validator], blank=True, null=True)

我的问题:

当我在 myurl/accounts/signup 中添加用户时,它告诉我 my_file 字段是必需的,即使我 select 一个文件。

allauth使用的signup.html模板没有

enctype="multipart/form-data"

添加后效果非常好