当用户在 Django 管理中激活时发送电子邮件

发布于 2024-09-13 11:50:27 字数 447 浏览 5 评论 0原文

我即将创建一个监控注册的网站,只允许某些人注册。毫无疑问,尽管我在注册表上写了任何文字,但仍有一些不适应的人会注册,所以我们会适度进行。

注册后,将创建 django.contrib.auth User 和个人资料,并向版主发送一封电子邮件。主持人将登录 Django 管理站点,检查他们是否被允许注册并将其帐户标记为活动状态。如果他们是试图溜进去的恶棍,该帐户将被删除。

我将使用 recapcha 来尝试阻止自动尝试。

我想在帐户被激活或删除时发送一封电子邮件,让帐户持有人知道他们的帐户发生了什么事,他们可以登录,或者让他们知道我们知道他们在做什么,他们应该别再犯傻了。

我怀疑这与信号有关,但坦率地说,我不知道这实际上适合哪里,因为我使用的是 django.contrib.auth 提供的预制模型。

任何提示、线索或代码都将被慷慨地接受。

I'm about to create a site that has monitored registration in that only certain people are allowed to register. Undoubtedly some misfits will register despite any writing I put above the registration form so we're going with moderation.

Upon registration a django.contrib.auth User and profile will be created and an email will be sent to the moderator. The moderator will log into the Django admin site, check they're somebody who is allowed to register and mark their account active. If they're some miscreant trying to slip in, the account would be deleted.

I'll use recaptcha to try and stop automated attempts.

I would like to fire off an email when an account is activated or deleted to let the account holder know what has happened to their account and that they can either log in, or let them know we know what they're up to and they should stop being silly.

I suspect this has something to do with signals but I frankly don't have a clue where this would actually fit in, given that I'm using a prefab model provided from django.contrib.auth.

Any tips, clues or code are all graciously accepted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-09-20 11:50:27
from django.db.models.signals import post_save
from django.contrib.auth.models import User

def send_user_email(sender, instance=None, **kwargs):
    if kwargs['created']:
        #your code here
post_save.connect(send_user_email, sender=User)

像这样的东西应该有效。 这里是文档。

from django.db.models.signals import post_save
from django.contrib.auth.models import User

def send_user_email(sender, instance=None, **kwargs):
    if kwargs['created']:
        #your code here
post_save.connect(send_user_email, sender=User)

Something like this should work. Here are the docs.

掩耳倾听 2024-09-20 11:50:27

您想查看信号

  • 激活帐户后,您应该使用 预保存。您可以将当前用户与数据库中的现有实例进行比较:检查实例是否存在,检查前一个实例是否 active=False,检查新实例是否 active=True,然后 发送电子邮件
  • 删除帐户时,使用 pre_delete 。如果您使用 post_delete,您将无法访问该电子邮件。

You want to take a look at the Signals.

  • When the account is activated, you should use a pre_save. You can compare the current User with the existing instance at the database: check that the instance exists, check the previous was active=False, check that the new one is active=True, then send an email.
  • When the account is deleted, use a pre_delete. If you use a post_delete you won't be able to access to the email.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文