Sending emails when a user is activated in the Django admin

廉价感情. 提交于 2021-01-27 08:25:15

问题


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.


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/3441725/sending-emails-when-a-user-is-activated-in-the-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!