0

I'm setting up a custom-user registration form to create an account on my Django project. I feel I'm missing something very simple that keeps me from registering for some reason. My registration form demands 4 fields to be filled before a new user can register. Of these 4 fields, two are just a confirmation of the email address.

Here is a copy of my form.py:

class UserRegisterForm(forms.ModelForm):
    username = forms.CharField(label='Username')
    email = forms.EmailField(label='Email Address')
    email2 = forms.EmailField(label='Confirm Email')
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = CustomUser
        fields = ['username', 'email', 'email2', 'password']

    def clean_email(self):
        email = self.cleaned_data.get('email')
        email2 = self.cleaned_data.get('email2')
        if email != email2:
            raise forms.ValidationError("Emails do not match")

And here is a copy of my view.py:

def register_view(request):
    next = request.GET.get('next')
    form = UserRegisterForm(request.POST or None)
    if form.is_valid():
        user = form.save(commit=False)
        password = form.cleaned_data.get('password')
        user.set_password(password)
        user.save()
        new_user = authenticate(username=user.username, password=password)
        login(request, new_user)
        if next:
            return redirect(next)
        return redirect('/home/')
    context = {'form':form, "title":"register"}
    return render(request, "register.html", context)

Here is my html file:

<h2><center><font color="white">Register Here:</font></center></h2>
    <form method="POST"><center>
        {% csrf_token %}
        {{form}}
      <input type="submit" />
    </center></form>

Whenever I try to register as a new user, my form validation error is raised as is says that my two emails do not match (I copy-paste them to make sure they are identical).

I feel I'm missing something that is right under my nose...

Cheers to all of you and thank you very much.

chenard612
  • 101
  • 2
  • 15

2 Answers2

0

try this

def register_view(request):
    next = request.GET.get('next')
    form = UserRegisterForm(request.POST or None)
    if form.is_valid():
       email = form.cleaned_data.get('email')
       email1 = form.cleaned_data.get('email1')
        if email1 == email 
           user = form.save(commit=False)
           password = form.cleaned_data.get('password')
           user.set_password(password)
           user.save()
           new_user = authenticate(username=user.username, password=password)
           login(request, new_user)
              if next:
                 return redirect(next)
          else:
             ….
  • Thank you very much Mary, it solved my problem for good. It is indeed much more logical to have this condition in the views instead of in the form. Have a good one! – chenard612 Mar 16 '20 at 04:31
  • Happy to help :) –  Mar 16 '20 at 04:52
0

    def clean(self):
        cleaned_data = super(UserRegisterForm, self).clean()
        email = cleaned_data.get('email')
        email2 = cleaned_data.get('email2')
        if email != email2:
            self.add_error("email", "Emails do not match")

The problem is that in clean_email method you have email2 to be always None. To compare two or more fields use clean method

Andrey Nelubin
  • 3,084
  • 1
  • 20
  • 25