从SIgnUp表单中删除标签

时间:2016-10-02 06:03:02

标签: django django-forms django-allauth

我正在尝试扩展默认表单并删除Django-allauth注册表单的标签。大多数标签都已成功删除,但我无法删除电子邮件字段的标签。

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

呈现SignUp表单 enter image description here

1 个答案:

答案 0 :(得分:0)

也许是因为你忘了添加

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

进入SignupForm字段?

还要将“电子邮件”添加到

fields = ('first_name', 'last_name',  'gender', 'birthday', 'location', 'email')