The Django password reset form is not working, where is the problem?

Viewed 28

My motive is to reset the password by sending an email to the user. When the user will click on the link that sends by mail will open an Html page, which will have a password and confirm password form. The email sending system is working well, when I click on the link that sends by mail opens a password reset form. But the problem is, the password reset form is not working. Password not being changed. Where did the actual problem occur? Password not being change. Where did the actual problem occur? Please give me a relevant solution...

helper.py:

from django.core.mail import send_mail
from django.conf import settings 


def send_forget_password_mail(email , token ):
    subject = 'Your forget password link'
    message = f'Hi , click on the link to reset your password http://127.0.0.1:8000/changepassword/{token}/'
    email_from = settings.EMAIL_HOST_USER
    recipient_list = [email]
    send_mail(subject, message, email_from, recipient_list)
    return True

views.py:

def ChangePassword(request, token):

    context = {}

    try:
        profile_obj = User.objects.filter(forget_password_token=token).first()

        print(profile_obj)

        if request.method == 'POST':
            new_password = request.POST.get('new_password')
            confirm_password = request.POST.get('reconfirm_password')
            user_id = request.POST.get('user_id')

            if user_id is None:
                messages.warning(request, 'No user id found.')
                return redirect(f'/changepassword/{token}/')

            if  new_password != confirm_password:
                messages.warning(request, 'both should  be equal.')
                return redirect(f'/changepassword/{token}/')

            return redirect('login_user')

        context = {'user_id' : profile_obj.user.id}


    except Exception as e:
        print(e)

    
    return render(request, 'new_password.html', context)

def forgot_password(request):

    try:
        if request.method == 'POST':
            email = request.POST.get('email')

            if not User.objects.filter(email=email).first():
                messages.warning(request, 'Not user found with this username.')
                return redirect('forgot_password')

            user_obj = User.objects.get(email = email)
            token = str(uuid.uuid4())

            send_forget_password_mail(user_obj.email , token)

            messages.success(request, 'an email is send.')
            return redirect('forgot_password')
            
    except Exception as e:
        print(e)

    context ={

    }
    return render(request,'forgot.html', context)

urls.py:

path('changepassword/<token>/', views.ChangePassword, name="changepassword"),
path('forgot_password/', views.forgot_password, name="forgot_password"),

forgot.html:

<form method="POST" action="#!" class="needs-validation mt-5" style="font-size:13px;" novalidate="" autocomplete="off">
{% csrf_token %} 
    <input id="email" type="email" class="form-control " style="font-size:13px;" name="email" value="" required autofocus>
    <button type="submit" class="btn btn-outline-dark ms-auto" style="font-size: 12px;"> Send Link </button>
</form>

new_password.html

<form action="" method="POST" class="needs-validation poppins_font" style="font-size: 13px;" novalidate=""autocomplete="off">
{% csrf_token %}

    <input id="password" style="font-size: 13px;" type="password" class="form-control" name="new_password" required>
    <input id="confirm_password" style="font-size: 13px;" type="password" class="form-control" name="reconfirm_password" required>

    <input type="hidden" name="user_id" value="{{user_id}}">

    <button type="submit" class="btn btn-outline-dark ms-auto" style="font-size: 12px;">Set</button>

</form>

models.py:

class User(AbstractUser):

    email = models.EmailField( max_length=150,unique=True,error_messages={"unique":"The email must be unique."})

    forget_password_token = models.CharField(max_length=100, blank=True, null=True)
1 Answers

I think the problem is that you're not saving the password to the database, and therefore the password isn't changed. Try something along the lines of:

def ChangePassword(request, token):

    context = {}

    try:
        profile_obj = User.objects.filter(forget_password_token=token).first()

        print(profile_obj)

        if request.method == 'POST':
            new_password = request.POST.get('new_password')
            confirm_password = request.POST.get('reconfirm_password')
            user_id = request.POST.get('user_id')

            if user_id is None:
                messages.warning(request, 'No user id found.')
                return redirect(f'/changepassword/{token}/')

            if  new_password != confirm_password:
                messages.warning(request, 'both should  be equal.')
                return redirect(f'/changepassword/{token}/')
                 

            else:
                profile_obg.password = new_password
                profile_obg.save()



            return redirect('login_user')

        context = {'user_id' : profile_obj.user.id}


    except Exception as e:
        print(e)

    
    return render(request, 'new_password.html', context)

Notice the added else statements, which means "if no error, save the password to the database"

Related