MWit
MWit

Reputation: 85

Django-allauth and custom Signup form

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. There are no SQL error messages or any form error messages.

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.

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

Also, here is the custom user model (which I just built off AllAuths version)

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?

Upvotes: 1

Views: 2114

Answers (1)

MWit
MWit

Reputation: 85

After a few days off, I returned and found the silly mistake!

In my Form.py the password field is called 'password1' not password!

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!

Upvotes: 2

Related Questions