0

I'm trying to extend the default form and remove the labels for Django-allauth signup form. Most labels are removed successfully, but I'm not able to remove the label for the email field.

forms.py

from django import forms
from .models import Profile


class SignupForm(forms.ModelForm):
     gender = forms.CharField(max_length=1, label='Gender')
     first_name = forms.CharField(max_length=50, label='First Name')
     last_name = forms.CharField(max_length=50, label='Last Name')
     birthday = forms.CharField(max_length=50, label='Birthday')
     location = forms.CharField(max_length=50, label='Location')

     def __init__(self, *args, **kwargs):
         super(SignupForm, self).__init__(*args, **kwargs)
         self.fields['first_name'].widget.attrs.update({'autofocus': 'autofocus'})

         #remove labels for fields
         for field_name in self.fields:
            field = self.fields.get(field_name)
            field.widget.attrs['placeholder'] = field.label
            field.label =''


    class Meta:
         model = Profile
         fields = ('first_name', 'last_name',  'gender', 'birthday', 'location')

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

         # Save your profile
         profile = Profile()
         profile.user = user
         profile.birthday = self.cleaned_data['birthday']
         profile.location = self.cleaned_data['location']
         profile.gender = self.cleaned_data['gender']
         profile.save()

models.py

from django.db import models
from django.contrib.auth.models import User


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    birthday = models.DateField(null=True, blank=True)
    #first_name = models.CharField(max_length=100)
    #last_name = models.CharField(max_length=100)
    location = models.CharField(max_length=100)
    timestamp = models.DateTimeField(auto_now_add= True, auto_now=False)
    GENDER_CHOICES = (
        ('m', 'Male'),
        ('f', 'Female'),
    )
    # gender can take only one of the GENDER_CHOICES options
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES,
                              verbose_name='Gender')

    def __str__(self):
        return self.user.first_name

Rendered SignUp form enter image description here

zan
  • 594
  • 2
  • 8
  • 20

1 Answers1

0

Maybe it is because you forgot adding

email = forms.EmailField(label='Email')

into the SignupForm fields?

also add 'email' into

fields = ('first_name', 'last_name',  'gender', 'birthday', 'location', 'email')
bluebird_lboro
  • 601
  • 5
  • 17
  • Don't think so as the email field label shows up (see image above). I'll try this out anyway. Thks. – zan Oct 02 '16 at 07:46
  • Unfortunately, the email label still appears, with it's placeholder set to None. – zan Oct 02 '16 at 11:04
  • Hi, is this email field belong to user model or profile model? I don't see the email see this email field in the profile model form fields, but you use `profile.email = self.cleaned_data['email']` to set the email field. So I am wondering what is you profile model looks like..thanks – bluebird_lboro Oct 05 '16 at 08:44
  • Hi lboro, I've included the models. The profile.email is not required as it is handed in the [BaseSignupForm](https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py). Someone has commented about this issue as seen [here](https://github.com/pennersr/django-allauth/issues/1501) – zan Oct 05 '16 at 15:05