django-allauth:自定义注册无法保存

时间:2016-11-08 17:28:43

标签: django django-forms registration django-allauth

我正在尝试创建自定义注册,填充与Profile具有OneToOne关系的User模型的字段。我的表单显示正确但保存只会导致页面刷新。使用django-allauth附带的标准表单可以正常工作。

在我的forms.py我有一个使用def signup(self)的自定义表单,原始创建者发布了here。这个功能似乎永远不会被调用。

settings.py

# allauth settings
SITE_ID = 1
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5

ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_USERNAME_REQUIRED = False

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"

ACCOUNT_FORMS = {'signup': 'accounts.forms.SignupForm'}

forms.py

class SignupForm(forms.ModelForm):

    email = forms.EmailField(widget=forms.TextInput(
        attrs={'type': 'email',
               'placeholder': _('E-mail address')}))
    password1 = PasswordField(label=_("Password"))
    password2 = PasswordField(label=_("Password (again)"))

    class Meta:
        model = Profile
        fields = '__all__'


    def signup(self, request, user):
        print "hi"
        print request.POST
        print user
        new_profile = Profile()
        new_profile.user = user
        new_profile['field1'] = self.cleaned_data['field1']
        new_profile['field2'] = self.cleaned_data['field2']

models.py

class Profile(models.Model):

    user = models.OneToOneField(User)

    field1 = models.CharField(max_length=255)
    field2 = models.CharField(max_length=255)

1 个答案:

答案 0 :(得分:0)

你需要在最后调用save()

def signup(self, request, user):
        .....
        new_profile.save()