在下面的代码中,当“忘记密码”运行时,它会从用户处获取“电子邮件”输入并生成令牌,使用该电子邮件和令牌,用户可以更改其密码,但我想要的是:用户只需要输入“new_password”和“reconfirm_password”而不是电子邮件来更改密码,电子邮件应该从“忘记密码视图”中获取,以下代码在执行时会出现此错误:无法访问与值不关联的局部变量“email”
views.py
class ChangePasswordView(APIView):
def get(self, request, token, email):
context = {}
try:
profile_obj = UserAccount.objects.filter(
forget_password_token=token).first()
if profile_obj:
context['email'] = email
else:
return JsonResponse({'message': 'Invalid token.'})
except Exception as e:
print(e)
return JsonResponse({'message': 'An error occurred.'})
return JsonResponse(context)
def post(self, request, **kwargs):
try:
context = {}
profile_obj = UserAccount.objects.filter(
email=email).first()
if profile_obj:
context['email'] = email
new_password = request.data.get('new_password')
confirm_password = request.data.get('reconfirm_password')
email = request.data.get('email')
print(new_password)
if email is None:
return JsonResponse({'message': 'No user email found.'})
if new_password != confirm_password:
return JsonResponse({'message': 'Both passwords should be equal.'})
user_obj = UserAccount.objects.get(email=email)
user_obj.set_password(new_password)
user_obj.save()
return JsonResponse({'message': 'Password changed successfully.'})
except Exception as e:
print(e)
return JsonResponse({'message': 'An error occurred.'})
@method_decorator(csrf_exempt, name='dispatch')
class ForgetPasswordView(View):
def post(self, request):
try:
data = json.loads(request.body)
email = data.get('email')
if not UserAccount.objects.filter(email=email).exists():
return JsonResponse({'message': 'No user found with this email.'})
user_obj = UserAccount.objects.get(email=email)
token = str(uuid.uuid4())
user_obj.forget_password_token = token
user_obj.save()
send_forget_password_mail(user_obj.email, token)
return JsonResponse({'message': 'An email has been sent.'})
except Exception as e:
print(e)
return JsonResponse({'message': 'An error occurred.'})
return JsonResponse({'message': 'Something went wrong.'})
1条答案
按热度按时间dxxyhpgq1#
我个人使用
rest_framework_simplejwt
生成一个令牌,并发送一封带有URL和令牌的电子邮件到忘记密码请求的电子邮件。然后在新的URL上提交新密码并解码令牌以获取用户并更改密码。用户模型
在这里,您可以访问刷新和访问令牌,但我们需要的是访问令牌。
忘记密码查看
您可以将**localhost:8000/set-password/**替换为设置密码端点的反向URL。找到用户后,将生成令牌并将其发送到用户的电子邮件。
设置密码视图
对令牌进行解码以获得更改的用户ID和用户密码,否则给出正确的响应。