password not the same but still the same

I am doing a user registration function for my webpage but when I try to create a user I get an error message saying that the repeated password is not the same even when it is clearly is that. what is wrong?

def anv(request):





if r.POST:


    if form.is_valid():


#       ferror=form.error_messages


if ids:
    for l in ids:

return render(request,'registration/signup.html',{'form':form,'users':getUsers(),'username':h1,'p1':p1,'p2':p2,'ids':ids,'sa':sa,'ferror':ferror})

class CreateUser(UserCreationForm):

password2=forms.CharField(label='confirm password',widget=forms.PasswordInput) 

def username_clean(self):  
    username = self.cleaned_data['username'].lower()  
    new = User.objects.filter(username = username)  
    if new.count():  
        raise ValidationError("User Already Exist")  
    return username  

def clean_password2(self):  
    password1 = self.cleaned_data['password1']  
    password2 = self.cleaned_data['password2']  

    if password1 and password2 and password1 != password2:  
        raise ValidationError("Password don't match")
    return password2  

def save(self, commit = True):  
    user = User.objects.create_user(  

    return user

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. That means you will have a line of ```, then your code, then another line of ```. (When posting code from multiple files, it’s generally most useful to do this for each file individually.) This forces the forum software to keep your code properly formatted. This is also the recommended process when posting templates, error messages, and other pre-formatted text.

Your view is distorted by the forum formatting - you don’t need to repost your code, edit your post and add the ``` before and after your view and your forms.

I’m also curious about the regex you’re using in that view. It doesn’t look like any regex syntax that I’m familiar with as a legal Python regex. (But since I don’t think it’s matching anything, the substitution isn’t going to happen, so I don’t see that as a problem.)

When you’re not sure what’s happening, it can be helpful to both look at the data being posted by the form back to the server, and to print the data being processed within the view and other functions. At a minimum, I would recommend printing both password1 and password2 in the clean_password2 method to verify what you’re getting.

thanks for the input. There is no regex in the view. I have used breakpoints to analyze the problem and the problem is that is_valid() function returns false because it does not think that the passwords are the same even when they are.

so,here is how I solved the problem. I reimplemented the whole form, looking up user, check password etc, so now it works.