django 从 queryset 发送邮件
我正在尝试使用Django的send_mail方法。不过我遇到了两个问题。第一个问题是如何把一个邮件地址的列表转换成CSV格式,以便用作收件人。第二个问题是如何从通知模型中获取用户的邮件列表。
我的模型
class notifications(models.Model):
notID = models.AutoField(primary_key=True)
notSubject = models.CharField(max_length=100)
notBody = models.TextField()
notDate = models.DateField(auto_now_add=True)
notRecepients = models.ManyToManyField(UserProfile, related_name='recepients')
notPoster = models.ForeignKey(UserProfile,related_name='notposter')
deleted = models.BooleanField()
def __unicode__(self):
return u'%s ' % ( self.notSubject)
class UserProfile(User):
onames = models.CharField(max_length=30, blank=True)
phoneNumber = models.CharField(max_length=15, blank=True)
regNo = models.CharField(max_length=15)
image = models.ImageField(max_length=100,upload_to='photos/%Y/%m/%d', blank=True, null=True, default='photos/2010/03/placeholder.jpg')
course = models.CharField(max_length=30, blank=True, null=True)
timezone = models.CharField(max_length=50, default='Africa/Nairobi')
smsCom = models.BooleanField()
mailCom = models.BooleanField()
class notifications_sms_mail(models.Model):
notId = models.OneToOneField(notifications)
sms = models.BooleanField()
email = models.BooleanField()
我的视图
def notif_sms_mail(request):
user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
mailform = notif_sms_mail_Form(request.POST)
if mailform.is_valid():
notid=request.POST['notId']
mailform.save()
notif = notifications.objects.get(deleted='0', notID = notid)
rec = (UserProfile.objects.values('email')).filter(mailCom='1')
mail = notifications_sms_mail.objects.get(email='1', notId=notid)
if notif.notID == mail.notId:
send_mail(notSubject, notBody, 'from ****@gmail.com',rec, fail_silently=False)
1 个回答
2
这段代码是用来处理一些特定的任务的。它可能涉及到一些数据的输入和输出,或者是对数据进行某种操作。具体来说,代码块中的内容可能包含了变量的定义、循环、条件判断等基本的编程结构。
如果你是编程新手,可以把这段代码想象成一个简单的食谱。食谱告诉你需要哪些材料(变量),然后一步一步教你怎么做(代码的执行过程)。每一步都很重要,缺少了哪一步,最后的结果可能就不对了。
总之,这段代码的目的是为了让计算机按照我们设定的步骤去完成某个任务。理解这些基本的概念后,你就能更好地掌握编程的技巧了。
UserProfile.objects.filter(mailCom='1').values_list('email', flat=True)