我在创建通向视图并将 account.is_active 设置为 True 的确认电子邮件时遇到问题请记住,我有一个自定义用户模型,它从用户模型中抽象出来,令牌生成器工作正常,我得到了电子邮件我点击链接,它说帐户已激活但实际上没有发生任何事情我认为问题出在激活视图中
我的模型.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email",max_length=60, unique=True)
username = models.CharField(max_length=60,unique=True)
phone = models.CharField(max_length=60,unique=True)
date_joined = models.DateTimeField(verbose_name="date joined",auto_now_add=True)
last_login = models.DateTimeField(verbose_name="last login",auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email','phone']
objects = MyAccountManager()
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
令牌.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, account, timestamp):
return (
six.text_type(account.pk) + six.text_type(timestamp) +
six.text_type(account.is_active)
)
account_activation_token = TokenGenerator()
视图.py
#active view==========================================
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
account = Account.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, Account.DoesNotExist):
account = None
if account is not None and account_activation_token.check_token(account, token):
account.is_active = True
account.save()
login(request, account, backend="users.backends.AccountAuthBackend")
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
我曾经使用默认的 django 用户以相同的方式工作,但现在当我更改为我的自定义用户模型并使用 rest 框架 api 时,这发生了