如果 Django 用户的活动状态发生更改,则向其发送电子邮件
我建立了一个网站,某些部分需要会员资格。这是一个俱乐部网站,因此要成为网站上的会员,您必须成为现实生活中的会员。计划是让俱乐部中的某人检查新会员(我可能会让系统在用户注册时向他们发送一封电子邮件),然后管理员检查用户记录下的active
复选框在 Django 管理中并保存用户。
我试图克服的问题是我们需要通知新的有效用户何时可以开始使用他们的帐户。显然,手动发送电子邮件很麻烦。
有没有办法挂钩 save()
逻辑,检查记录的 active
状态是否已更改,如果已激活,则向该用户发送一封电子邮件告诉他们他们现在可以登录了吗?
我掌握所有电子邮件逻辑,我只需要知道将其放在哪里。
我意识到还有其他测试方法(在 cron 式作业的 active==True
帐户上检查 last_login==None
),但我希望通知几乎足够即时。
I've built a site that requires membership for some portions. It's a club website so to be a member on the website, you have to be a member in real life. The plan is to have somebody from the Club check for new members (I'll probably have the system send them an email when a user signs up) and then the admin checks the active
checkbox under the user's record in the Django admin and saves the user.
The problem I'm trying to overcome is we need new, valid users to be notified as to when they can start using their account. Obviously manually sending an email is cumbersome.
Is there a way to hook into the save()
logic, check if the record's active
state has changed, and if it has been activated, send that user an email telling them they can now log in?
I'm on top of all the email logic, I just need to know where to put it.
I realise there are other ways of testing (check for last_login==None
on active==True
accounts on a cron-style job) but I'd like notifications to be near-enough instant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您需要使用 django 信号,特别是 post_save()。正如您可能猜到的那样,在保存模型后调用 get,然后您可以实现所需的任何保存后功能(即写入数据库后)。
Yes, you need to use django signals, specifically post_save(). This, as you probably guessed, get's called after the save of your model and you can then implement whatever post save functionality (that is, post write to the database) you require.
好的,5年后,但这对我来说适用于 django 1.8 和 python 2.7
上下文是:用户创建一个新帐户,然后管理员收到一封电子邮件来验证用户,并在管理员进行更改时将其更改为 True,用户收到电子邮件告诉他现在可以登录。
抱歉我的英语不好。
我在 models.py 文件中使用信号,但您可以在 signals.py 文件中使用它
ok 5 years latter but this works for me with django 1.8 and python 2.7
The context is: the user create a new account then the admin receives an email to verify the user and chage active to True when the admin makes the change the user receives an email telling that now he can log in.
Sorry for my bad english.
I use the signals in models.py file but you can use it in a signals.py file
这尚未经过测试,但这是我所做的类似操作的修改版本:
希望这有帮助!
This has not been tested but here is a modified version of something I do that is similar:
Hope this helps!