Подтверждающее письмо работает на localhost, но не на ngrok django
У меня есть этот код. Когда я использую его на localhost, он работает, но когда я использую его с ngrok, он показывает ошибку 400. Это странно, потому что если я перезагружаю страницу дважды, ошибка исчезает, и она загружает url, но показывает: Activation link is invalid!.
В views.py:
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = CustomUser.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, CustomUser.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return HttpResponse('Activation link is invalid!')
mail_subject = 'Activate your account.'
message = render_to_string('twc/acc_active_email.html', {
'user': user,
'domain': request.get_host(),
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
В urls.py:
path('activate/<uidb64>/<token>/', views.activate, name="activate"),
Наконец, acc_active_email:
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}