So i am trying to make a reset password functionality in django but getting this error, can't debug it.
So this is the error
Traceback (most recent call last):
File "/home/aman/.local/share/virtualenvs/stet-sikkim-project-D4VwjO1D/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/aman/.local/share/virtualenvs/stet-sikkim-project-D4VwjO1D/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/aman/.local/share/virtualenvs/stet-sikkim-project-D4VwjO1D/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/aman/.local/share/virtualenvs/stet-sikkim-project-D4VwjO1D/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/aman/.local/share/virtualenvs/stet-sikkim-project-D4VwjO1D/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/home/aman/Desktop/stet-sikkim-project/authentication/views.py", line 184, in post
user = request.objects.filter(email=email)
Exception Type: AttributeError at /authentication/request-password
Exception Value: 'WSGIRequest' object has no attribute 'objects'
This is the code,
class RequestPasswordResetEmail(View):
def get(self, request):
return render(request, 'authentication/reset-password.html')
def post(self, request):
email = request.POST['email']
context = {
'values': request.POST
}
if not validate_email(email):
messages.error(request, 'Please enter a valid email')
return render(request, 'authentication/reset-password.html', context)
current_site = get_current_site(request)
user = request.objects.filter(email=email)
if user.exists():
email_contents = {
'user': user[0],
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user[0].pk)),
'token': PasswordResetTokenGenerator().make_token(user[0]),
}
link = reverse('reset-user-password', kwargs={
'uidb64': email_contents['uid'], 'token': email_contents['token']})
email_subject = 'Password Reset STET Sikkim'
reset_url = 'http://'+current_site.domain+link
email = EmailMessage(
email_subject,
'Please click the link below to reset your password, ignore if you did not made this request\n'+reset_url,
'[email protected]',
[email],
)
email.send(fail_silently=False)
messages.success(
request, 'We have sent you an email to reset your password')
return render(request, 'authentication/reset-password.html')
Why is this error happening, i can't find any solution on stack overflow and unable to debug, help if see any mistake in the code.
user = request.objects.filter(email=email)
request is not a models, you need to user models ( the models you want to filter )
user = User.objects.filter(email=email)