简体   繁体   English

Django-allauth和自定义注册表单

[英]Django-allauth and custom Signup form

Cant seem to find where I am going wrong here. 无法在这里找到我要去的地方。 There are two issues, but I feel like a simple config change will fix both. 有两个问题,但是我觉得简单的配置更改就可以解决这两个问题。

When displaying the below code the password field does not get displayed. 显示以下代码时,不会显示密码字段。 Crispy-Forms generates a warning Could not resolve form field 'password' 脆皮表格生成警告无法解析表格字段“密码”

When I add a password field manually, and I submit the form, nothing happens. 当我手动添加密码字段并提交表单时,没有任何反应。 Ie. 就是 I am returned to the signup page with the form filled out. 我将返回填写表单的注册页面。 But save_user() or the signin() function never gets called. 但是save_user()或signin()函数永远不会被调用。 There are no SQL error messages or any form error messages. 没有SQL错误消息或任何形式的错误消息。

Below are the settings: 以下是设置:

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'

Both ACCOUNT_SIGNUP and ACCOUNT_ADAPTER point to the right files as I am able to make changes to see the, reflected. ACCOUNT_SIGNUP和ACCOUNT_ADAPTER都指向正确的文件,因为我可以进行更改以反映出来。

This forms.py 这个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()

I have created a form just as outlined here How to customize user profile when using django-allauth 我已经创建了一个表单,这里概述了如何使用django-allauth时如何自定义用户配置文件

Also, here is the custom user model (which I just built off AllAuths version) 另外,这是自定义用户模型(我是基于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})

Any help or points in the right direction would be wonderful! 任何帮助或指向正确方向的事情都太好了!

Thanks in advanced! 提前致谢!

After a few days off, I returned and found the silly mistake! 休息几天后,我回来了,发现了这个愚蠢的错误!

In my Form.py the password field is called 'password1' not password! 在我的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()

Great days! 美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django-allauth自定义社交注册表单 - Django-allauth custom social signup form Django-Allauth - 自定义用户模型和自定义注册表单的错误 - Django-Allauth - Errors with custom User model and custom Signup form 具有keyOrder的django-allauth自定义社交登录注册表单 - django-allauth Custom Sociallogin Signup Form with keyOrder 如何在Django-Allauth表单上覆盖.signup() - How to Override .signup() on Django-Allauth Form 使用django-allauth在自定义注册表单中添加带有自定义表单错误的返回form_invalid(或其他内容) - Adding returning form_invalid (or something) with custom form errors in custom signup form with django-allauth django-allauth 自定义注册表单。 如何添加带有图像上传I(图像字段)的字段? - django-allauth Custom signup form. How to add a field with image uploadI(image field)? 在 django-allauth 中使用自定义注册表单时外键约束失败 - Foreign Key Constraint failed when using custom signup form in django-allauth django-allauth注册表单中用户的外部模型 - foreign model in User in django-allauth signup form django-allauth 不同用户类型的多重注册表单 - django-allauth multiple signup form for difference user type NoReverseMatch at / accounts / signup / django-allauth错误 - NoReverseMatch at /accounts/signup/ django-allauth error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM