hello, in my django project I am trying to receive mail from the terminal during registration and verify it. the token and user id match but it redirects to the activation_invalid page every time I try. when I check with print for check_token, the token is invalid.
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
class TokenGenerator(PasswordResetTokenGenerator):
pass
account_activation_token = TokenGenerator()
views.py
from django.contrib.auth.views import LoginView
from django.shortcuts import render, redirect
from .forms import SignupForm
from .models import User
from django.http import HttpResponse
from django.views.generic import CreateView
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail
from django.urls import reverse
from django.conf import settings
from .tokens import account_activation_token
from django.contrib.auth import login
from django.utils.http import urlsafe_base64_decode
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes, force_str
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
@login_required
def home(request):
return render(request, 'firstlog/home.html', {'user': request.user})
class CustomLoginView(LoginView):
template_name = 'firstlog/login.html'
def get_success_url(self):
return reverse_lazy('home')
class SignupView(CreateView):
form_class = SignupForm
template_name = 'firstlog/register.html'
success_url = reverse_lazy('login')
def form_valid(self, form):
user = form.save(commit=False)
user.is_active = False # Kullanıcı doğrulama yapmadan aktif olmasın
user.save()
send_verification_email(user, self.request)
return super().form_valid(form)
@login_required
def profile_view(request):
return render(request, 'firstlog/profile.html', {'user': request.user})
def send_verification_email(user, request):
token = account_activation_token.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
activation_link = request.build_absolute_uri(reverse('activate', args=[uid, token]))
subject = 'Hesap Aktivasyonu'
message = f'Hesabınızı aktifleştirmek için linke tıklayın: {activation_link}'
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
def activate(request, uidb64, token):
try:
uid = force_str(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is None:
print("Kullanıcı bulunamadı.")
elif not account_activation_token.check_token(user, token):
print("Token geçersiz.")
if user is not None and account_activation_token.check_token(user, token):
print(f"User: {user.username}, Token: {token}, Valid: {account_activation_token.check_token(user, token)}")
user.is_active = True
user.is_email_verified = True
user.save()
login(request, user)
return redirect('home')
else:
print("Aktivasyon geçersiz.")
return render(request, 'firstlog/activation_invalid.html')
models.py
class User(AbstractUser):
email = models.EmailField(unique=True)
tshirt_color = models.CharField(max_length=30, blank=True, null=True)
is_email_verified = models.BooleanField(default=False)
def __str__(self):
return self.username