在Django中更改电子邮件时发送确认电子邮件
我目前正在使用django注册,它运行良好(有一些技巧)。当用户注册时,他必须检查他/她的邮件并单击激活链接。那很好,但是 如果用户更改了电子邮件怎么办?我想向他/她发送一封电子邮件,以确认他是电子邮件地址的所有者在Django中更改电子邮件时发送确认电子邮件,django,registration,django-registration,Django,Registration,Django Registration,我目前正在使用django注册,它运行良好(有一些技巧)。当用户注册时,他必须检查他/她的邮件并单击激活链接。那很好,但是 如果用户更改了电子邮件怎么办?我想向他/她发送一封电子邮件,以确认他是电子邮件地址的所有者 是否有一个应用程序、代码片段或某种可以节省我自己编写它的时间?我最近也遇到了同样的问题。我不喜欢仅仅为了这个而使用另一个应用程序/插件的想法 您可以通过收听用户模型的单曲(pre\u save,post\u save)和使用RegistrationProfile)来实现这一点: 信号
是否有一个应用程序、代码片段或某种可以节省我自己编写它的时间?我最近也遇到了同样的问题。我不喜欢仅仅为了这个而使用另一个应用程序/插件的想法 您可以通过收听
用户
模型的单曲(pre\u save
,post\u save
)和使用RegistrationProfile
)来实现这一点:
信号。py:
因此,每当
用户的电子邮件发生更改时,该用户将被停用,并向该用户发送一封激活电子邮件。试试这些?如果这些都不起作用,我的意思是花时间调整两个应用程序以使其协同工作,还不如花时间编写一个小视图来读取散列验证代码,并在用户模型类中添加一个名为“verification”的状态字段。在验证完成之前,用户将被锁定。对于post_save
,您不需要通过请求或类似的请求吗?其中一个缺陷可能是,如果用户错误地将其电子邮件地址更新为不正确的地址,则不会收到电子邮件,他们将被锁定?不喜欢让用户帐户处于非活动状态的想法
from django.contrib.sites.models import Site, RequestSite
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from registration.models import RegistrationProfile
# Check if email change
@receiver(pre_save,sender=User)
def pre_check_email(sender, instance, **kw):
if instance.id:
_old_email = instance._old_email = sender.objects.get(id=instance.id).email
if _old_email != instance.email:
instance.is_active = False
@receiver(post_save,sender=User)
def post_check_email(sender, instance, created, **kw):
if not created:
_old_email = getattr(instance, '_old_email', None)
if instance.email != _old_email:
# remove registration profile
try:
old_profile = RegistrationProfile.objects.get(user=instance)
old_profile.delete()
except:
pass
# create registration profile
new_profile = RegistrationProfile.objects.create_profile(instance)
# send activation email
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_profile.send_activation_email(site)