Письмо с подтверждением приходит в обычном режиме на test@hotmail.com. Но всякий раз, когда я нажимаю, появляется сообщение: «Ссылка активации недействительна!» когда-либо.
Может ли кто-нибудь мне помочь?
Views.py
def register(request):
if not request.user.is_authenticated:
if request.method == 'POST':
register_status = register_user(request)
if register_status != 1:
messages.error(request, register_status)
return redirect('register')
#Register Success
else:
user = get_user_model()
subject = 'Confirm your account'
current_site = get_current_site(request)
message = render_to_string('users/acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
from_email = 'myacount@gmail.com'
to_list = [from_email , 'test@hotmail.com']
send_mail(
subject,
message,
from_email,
to_list,
fail_silently=False,
)
messages.success(request, 'Please confirm your email')
return redirect('login')
else:
return render(request, 'users/register.html')
else:
messages.error(request, 'You are already logged in')
return redirect('index')
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
User = get_user_model()
user = User.objects.get(pk=uid)
print(user)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
)
account_activation_token = TokenGenerator()
url.py
from django.urls import path
from . import views
urlpatterns = [
path('activate/<slug:uidb64>/<slug:token>', views.activate, name='activate'),
]
registration.py
from django.contrib.auth import get_user_model
from usersettings.models import UserSettings
from datetime import datetime
User = get_user_model()
def register_user(request):
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
result = check_input(username, email, password, password2)
if result != 1:
return result
else:
return save_user(username, email, password, password2)
def check_input(username, email, password, password2, test_date):
if password != password2:
return 'Passwords do not much'
else:
if User.objects.filter(username=username).exists():
return 'Username is taken'
else:
if User.objects.filter(email=email).exists():
return 'Email is taken'
else:
# Success
return 1
def save_user(username, email, password, password2):
user = User.objects.create_user(username=username, password=password, email=email)
user.save()
UserSettings.objects.create(user=user)
return 1
acc_active_email. html
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
ссылка
http://localhost:8000/users/activate/PHByb3BlcnR5IG9iamVjdCBhdCAweDdmMTM4YzdiODMxOD4/5ew-d33b3465e38a871c4c06
Я пробовал много исправлений, но безуспешно. Я новичок в Django и также не нашел ничего, чтобы решить эту проблему в Интернете.
Может ли кто-нибудь помочь мне заставить работать это письмо с подтверждением?