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?

views.py:

def anv(request):

r=request
sa=True

form=UserCreationForm()

form=CreateUser()
h1=form['username'].help_text
p1=form['password1'].help_text
p1=re.sub(r'</*\w*>','',str(p1))
p2=form['password2'].help_text

ferror="checking"

if r.POST:

form=CreateUser()

    if form.is_valid():
        form.save()
    else:

form=CreateUser()

#       ferror=form.error_messages
        sa=False

ids=request.POST.getlist('ids')

if ids:
    for l in ids:
        User.objects.get(id=l).delete()

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

forms.py:

class CreateUser(UserCreationForm):

username=forms.CharField(label='username',min_length=5,max_length=150)
password1=forms.CharField(label='password',widget=forms.PasswordInput)
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(  
        self.cleaned_data['username'],  
        self.cleaned_data['email'],  
        self.cleaned_data['password1']  
    )

    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.