python-3.xdjangoreset-password

Django - How to Manually update/set/Reset password


I tried to reset password manually by passing key But it always says

AttributeError : 'str' object has no attribute 'get'

I googled many times even if i saw Manual Password with hiding password field Django but no luck favor me.

// In my form #forms.py
class UserResetPasswordForm(forms.Form):    

new_password1 = forms.CharField(
    label=_("New Password"),
    widget=forms.PasswordInput(attrs={"autocomplete":"off"}),
    strip=False,
    help_text=password_validation.password_validators_help_text_html(),
)

new_password2 = forms.CharField(
    label=_("New Password Confirmation"),
    strip=False,
    widget=forms.PasswordInput(attrs={"autocomplete":"off"}),
)

In Url

path('password-reset/confirm/<str:reg_key>/', accounts_view.myreset_password_confirm, name='myreset_password_confirm'),

In View #views.py

def myreset_password_confirm(request, reg_key):

user = User.objects.filter(activate_key=reg_key).first()    
            
if user is not None:
    if request.method == 'POST':
        form = UserResetPasswordForm(request.POST)
        if form.is_valid():
            #password1 = form.cleaned_data.get('new_password1')
            #password2 = form.cleaned_data.get('new_password2')
            password1 = request.POST['new_password1']
            password2 = request.POST['new_password2']

            if password1 and password2:
                if password1 != password2:
                    raise ValidationError("Your password didn't match.")
                password_validation.password_validators_help_text_html()
                return password2

            print(password2)
            
            user.activate_key = ''
            user.set_password(password2)
            user.save()

            print("Print User")
            print(type(user))

            messages.success(request, f"Your password for {user.email} has been reset successfully!")
            return HttpResponseRedirect('/')
    else:            
        form = UserResetPasswordForm()            
        return render(request, 'reset_password_confirm.html', {'form':form})
else:
    print("U R Boomed")
    messages.error(request, "Your requested URL is not Valid, Please try with valid URL.")
    return HttpResponseRedirect('/')

enter image description here

Please help, how to solve it?


Solution

  • You are returning return password2, which is not proper HttpResonse. I don't think you wanted to do that. Just delete this line.