0
votes

I added a new field name phone for phone number in my registration form, but now it throws error when i submit it throws

Cannot resolve keyword 'phone' into field. Choices are: address, date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, notifications, order, password, profile, user_permissions, username

this error i am using Django default usercreation forms here is my forms.py

class SignUpForm(UserCreationForm):
    phone_regex = RegexValidator(regex=r'^\+?1?\d{10}$', message="Inform a valid phone number.")
    email = forms.EmailField(max_length=254, required=True, help_text='Required. Inform a valid email address.')
    phone = forms.CharField(validators=[phone_regex], max_length=10, required=True,  help_text='Required. Inform a valid phone number.')
    class Meta:
        model = User
        fields = ('username', 'email', 'phone', 'password1', 'password2',)

    def clean_email(self):
        email = self.cleaned_data['email']
        qs = User.objects.exclude(pk=self.instance.pk).filter(email__iexact=email)
        if qs.exists():
            raise ValidationError('A user with this email address already exists')
        return email
    
    def clean_phone(self):
        phone = self.cleaned_data['phone']
        qs = User.objects.exclude(pk=self.instance.pk).filter(phone__iexact=phone)
        if qs.exists():
            raise ValidationError('A user with same phone number already exists')
        return phone

views.py

class SignUpView(View):
    form_class = SignUpForm
    template_name = 'register.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():

            user = form.save(commit=False)
            user.is_active = False # Deactivate account till it is confirmed
            user.save()

            current_site = get_current_site(request)
            subject = 'Activate Your MySite Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)

            messages.success(request, ('Please Confirm your email to complete registration.'))

            return render( request, 'activation_sent_success.html')

        return render(request, self.template_name, {'form': form})

any suggestion help will be appreciated, thank you

1

1 Answers

1
votes

The error is caused by phone field:

class Meta:
    model = User
    fields = ('username', 'email', 'phone', 'password1', 'password2',)
                                    ^^^^^^

In your case, fields in class Meta is used to specify which fields of your User model will be in your form. You can omit some fields of your model with it.

Your User model does not have a field named phone. Adding this field to your model will solve your problem and do not forget executing python manage.py makemigrations.

If your model has this field, then you might forget applying migrations with python manage.py makemigrations.