如何在新用户添加到 django 管理站点时向用户发送电子邮件? [英] How can I send an email to user on new user addition to django admin site?

查看:17
本文介绍了如何在新用户添加到 django 管理站点时向用户发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to send an email with login details to user emailaddress whenever Admin adds new user to admin site.

I know Django provides send_mail module for that,but I don't know where should I put this code and override some view to send automatic mails on new user addition.

from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)

How can i do it?

I tried putting this code in my models.py

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

    def email_new_user(sender, **kwargs):
        if kwargs["created"]:  # only for new users
            new_user = kwargs["instance"] 
            print new_user.email
            #send_mail('Subject here', 'Here is the message.', 'from@example.com',['to@example.com'], fail_silently=False)


post_save.connect(email_new_user, sender=User)

But its not printing anything on command line. I think the function is not getting called whenever i create a new user.Don't know why?

解决方案

You want to hook the post_save signal for the User model.

这篇关于如何在新用户添加到 django 管理站点时向用户发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆