【问题标题】:Adding a FileField to a custom SignupForm with django-allauth使用 django-allauth 将 FileField 添加到自定义 SignupForm
【发布时间】:2016-03-26 08:48:25
【问题描述】:

我有以下自定义 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 字段是必需的,即使我选择了一个文件。

【问题讨论】:

    标签: python django django-forms django-allauth django-1.8


    【解决方案1】:

    allauth使用的signup.html模板没有

    enctype="multipart/form-data"
    

    添加后,它就像一个魅力。

    【讨论】:

    • 你是如何将 enctype attr 添加到默认模板的?
    猜你喜欢
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2016-08-15
    • 1970-01-01
    • 2021-02-17
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多