所以我试图在django中设置重置密码功能,但得到了这个错误,无法调试。
这就是错误
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'
这是密码
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')
为什么会出现这个错误,我找不到任何解决堆栈溢出的方法,无法调试,如果看到代码中的任何错误,请帮助。
2条答案
按热度按时间i7uaboj41#
请求不是一个模型,你需要用户模型(你想要过滤的模型)
ljsrvy3e2#
我不知道你是否还需要一个答案,但我会把它放在这里为其他人在未来。
所以我猜你是想得到一个特定的电子邮件,以便为该特定用户生成密码重置令牌。但是上面的代码没有意义,因为没有模型来查找具有您提供的特定电子邮件的用户。要编写的正确代码行将是:
我已经添加了.first(),因为从你的位置参数判断,我猜你想要第一个结果,但你写它的方式错误User对象是可下标的,因此使用.first()
另外,还会出现另一个错误,说User没有属性'exists',所以我建议的补救措施是使用这行代码:
而不是你用的那个基本上就是说,如果用户的主键可用,则执行以下代码。
我知道这可能不是最好的解释,但我希望它能有所帮助。干杯!干杯!