当django admin创建用户时,将用户详细信息发送到他的电子邮件

时间:2016-01-20 06:46:57

标签: python django

我一直在尝试将所有用户数据发送到他/她提供的电子邮件,只要管理员通过django admin创建用户详细信息。我认为post_save信号应该用于此类进程。但是我的post_save信号不会在控制台上调用管理员创建后也无法发送所有用户详细信息。我做错了什么?

Models.py

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.core.mail import EmailMessage
from django.dispatch import receiver
from django.db import models

@receiver(post_save,sender=User)
def send_user_data_when_created_by_admin(sender, **kwargs):
    import inspect
    records = []
    for record in inspect.stack():
      print('record',record)
      records.append(record[3])
    if record[3]=='get_response':
      request = record[0].f_locals['request']
      first_name = request.POST.get('first_name')
      print('first name is',first_name)
      last_name = request.POST.get('last_name')
      address = request.POST.get('address')
      email = request.POST.get('email')
      html_content = "your first name:%s <br> last name:%s <br> address:%s"
      from_email = settings.DEFAULT_FROM_EMAIL
      message=EmailMessage('welcome',html_content %(first_name,last_name,address),from_email,[email])
      message.content_subtype='html'
      message.send()

class userbase(models.Model):
  first_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the first name of user"))
  last_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the last name"))
  address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address"))
  contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact"))
  email = models.EmailField(max_length=100,blank=True,null=True,help_text=("enter the email"))
  username = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the username"))
  password = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the strong password"))

  def __str__(self):
    return (self.username)

1 个答案:

答案 0 :(得分:2)

你应该将你的功能改为

@receiver(post_save,sender=UserProfile)
def send_user_data_when_created_by_admin(sender, instance, **kwargs):

      first_name = instance.user.first_name
      print('first name is',first_name)
      last_name = instance.user.last_name
      address = instance.address
      email = instance.user.email
      html_content = "your first name:%s <br> last name:%s <br> address:%s"
      message=EmailMessage(subject='welcome',body=html_content %(first_name,last_name,address),to=[email])
      message.content_subtype='html'
      message.send()

你的模型应该是这个

class UserProfile(models.Model):
  address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address"))
  contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact"))
  user = models.ForeignKey(User)

  def __str__(self):
    return (self.username)

因此,只要您想保存新对象,就可以使用此

user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
UserProfile.objects.create(user=user, contact=contact, address=address)
user.save()

其中username,email,password,first_name,last_name,contact和address是您创建的表单中各自字段值的cleaning_data。

您可以设计管理员在创建用户时强制创建UserProfile,方法是在 admin.py

中使用这些行
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from account.models import UserProfile


class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False


class UserProfileAdmin(UserAdmin):
    inlines = (UserProfileInline,)


admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), UserProfileAdmin)

如果您遇到任何错误,请告诉我