Как проверить код аутентификации, который пришёл на почту в джанго?
Он приходит на почту и также сохраняется в таблице EmailVerification с привязкой к пользователю
Сейчас мне выдаёт ошибку:
KeyError at /email/verify/
'code'
traceback
Internal Server Error: /email/verify/
Traceback (most recent call last):
File "C:\Users\User\.virtualenvs\authorization\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\.virtualenvs\authorization\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\.virtualenvs\authorization\lib\site-packages\django\views\generic\base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\User\.virtualenvs\authorization\lib\site-packages\django\views\generic\base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "G:\Важное\Сохранения\Django-Projects\authorization\users\views.py", line 69, in get
code = EmailVerification.objects.get(code=kwargs['code'])
KeyError: 'code'
views.py
class EmailVerificationView(CreateView):
form_class = EmailVerificationForm
template_name = 'users/email_verification.html'
def get(self, request, *args, **kwargs):
code = EmailVerification.objects.get(code=kwargs['code'])
user = User.objects.get(email=kwargs['email'])
email_verifications = EmailVerification.objects.filter(user=user, code=code)
if email_verifications.exists():
user.is_verified_email = True
user.save()
return super(EmailVerificationView, self).get(request, *args, **kwargs)
forms.py
class EmailVerificationForm(forms.ModelForm):
class Meta:
model = EmailVerification
fields = ['code']
models.py
class EmailVerification(models.Model):
code = models.CharField(max_length=6, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'EmailVerification for {self.user.email}'
def send_verification_email(self):
send_mail(
'Subject here',
f'Verification code: {self.code}',
'from@example.com',
[self.user.email],
fail_silently=False,
)