Django-allauth和自定义注册表单

时间:2017-05-05 19:28:55

标签: python django forms django-allauth

似乎无法找到我在这里出错的地方。有两个问题,但我觉得简单的配置更改将解决这两个问题。

显示以下代码时,密码字段不会显示。 Crispy-Forms会生成警告无法解析表单字段'password'

当我手动添加密码字段并提交表单时,没有任何反应。 IE浏览器。我将返回到注册页面并填写表格。但是永远不会调用save_user()或signin()函数。没有SQL错误消息或任何表单错误消息。

以下是设置:

ACCOUNT_SIGNUP_FORM_CLASS = 'house.users.forms.SignupForm'
AUTH_USER_MODEL = 'users.User'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'optional'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_ADAPTER = 'house.users.adapters.AccountAdapter'

ACCOUNT_SIGNUP和ACCOUNT_ADAPTER都指向正确的文件,因为我可以进行更改以查看,反映出来。

此forms.py

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)

def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__( *args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_show_labels = False
    self.helper.field_class = ''
    self.helper.layout = Layout(
        Field('first_name', placeholder='First Name', autocomplete='off'),
        Field('last_name', placeholder='Last Name', autocomplete='off'),
        Field('email', placeholder='Email', autocomplete='off'),
        Field('password', placeholder='Password', autocomplete='off'),

        Div(Submit('Register', 'Register', css_class='btn btn-primary block full-width m-b'), css_class='form-group'),
        HTML('<p class="text-muted text-center"><small>Already have an account?</small></p>'),
        Div(HTML('<a class ="btn btn-sm btn-white btn-block" href="' + reverse('account_login') + ' " > Login </a>'),css_class='form-group' )
    )

def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

我已经创建了一个表格How to customize user profile when using django-allauth

此外,这是自定义用户模型(我刚刚建立了AllAuths版本)

class User(AbstractUser):
    name = models.CharField(max_length=30, null=True)
    birthdate = models.DateField(null=True, blank=True)
    legal_name = models.CharField(max_length=50, blank=True, null=True)
    is_agent = models.NullBooleanField(default=False, null=True)
    short_bio = models.CharField(max_length=200, null=True, blank=True)
    contacts = models.ManyToManyField(ContactDetails)

def __str__(self):
    return self.first_name + ' ' + self.last_name

def get_absolute_url(self):
    return reverse('users:detail', kwargs={'username': self.username})

任何正确方向的帮助或分数都会很精彩!

先谢谢!

1 个答案:

答案 0 :(得分:2)

休息几天后,我回来后发现了这个愚蠢的错误!

在我的Form.py中,密码字段被称为'password1'而不是密码!

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)

def __init__(self, *args, **kwargs):
  super(SignupForm, self).__init__( *args, **kwargs)
  self.helper = FormHelper()
  self.helper.form_show_labels = False
  self.helper.field_class = ''
  self.helper.layout = Layout(
    Field('first_name', placeholder='First Name', autocomplete='off'),
    Field('last_name', placeholder='Last Name', autocomplete='off'),
    Field('email', placeholder='Email', autocomplete='off'),
    Field('password1', placeholder='Password', autocomplete='off'),

    Div(Submit('Register', 'Register', css_class='btn btn-primary block full-width m-b'), css_class='form-group'),
    HTML('<p class="text-muted text-center"><small>Already have an account?</small></p>'),
    Div(HTML('<a class ="btn btn-sm btn-white btn-block" href="' + reverse('account_login') + ' " > Login </a>'),css_class='form-group' )
)

def signup(self, request, user):
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.save()

美好的日子!