Вход в Django не удается после активации почты
Вот как происходит регистрация:
class SignUpView(FormView):
template_name = 'center-form.html'
form_class = SignUpForm
def form_valid(self, form):
if form.is_valid():
user = User.objects.create_user(
first_name=form.cleaned_data['first_name'],
last_name=form.cleaned_data['last_name'],
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password1'],
is_active=False,
)
activation_code = uuid.uuid4().hex
Activation.objects.create(code=activation_code, user=user)
activation_url = self.request.build_absolute_uri(
reverse('activate', kwargs={'code': activation_code})
)
send_mail(
'Activate account',
f'To activate your account, please follow the link below\n{activation_url}',
'Django',
[form.cleaned_data['email']],
fail_silently=False,
)
messages.success(
self.request,
'Please check your email for a message with the activation code.',
)
return redirect('index')
В итоге я получаю письмо, в котором содержится некий код активации. Нажав на ссылку, я получаю сообщение об успехе в результате:
class ActivationView(View):
@staticmethod
def get(request, code):
activation = get_object_or_404(Activation, code=code)
activation.user.is_active = True
login(request, activation.user)
activation.user.save()
activation.delete()
messages.success(request, 'You have successfully activated your account')
return redirect('index')
Однако, когда я пытаюсь войти в систему, используя электронную почту и пароль, использованные ранее, я получаю ошибку в виде:
Please enter a correct username and password. Note that both fields may be case-sensitive.
Форма - AuthenticationForm
, а вот вид:
class SignInView(LoginView):
template_name = 'center-form.html'
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
data.update({'form_title': 'Sign in'})
return data