我想为批次和部门与请求的批次和部门匹配的唯一用户发送邮件。批次和部门与用户处于OneToOne关系。邮件应该从此功能发送。那邮件应该是当布尔字段is_principal为true时发送。这是我的代码。
models.py
class Rep(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
dept = models.ForeignKey(Departement, default=0)
batch = models.ForeignKey(Batch, default=0)
semester = models.ForeignKey(Semester, default=0)
def __str__(self):
return str(self.user)
class Retest(models.Model):
semester = models.ForeignKey(Semester)
dept = models.ForeignKey(Departement)
batch = models.ForeignKey(Batch)
date = models.DateField(blank=True, null=True)
subject = models.ForeignKey(Subject)
name = models.CharField(max_length=50)
admnno = models.CharField(max_length=50)
reason = models.CharField(max_length=50)
proof = models.CharField(max_length=200)
is_hod = models.BooleanField(default=False)
is_principal = models.BooleanField(default=False)
notify = models.BooleanField(default=False)
is_sure = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse( 'retest:retestform')
def __str__(self):
return self.name
views.py
def accepted(request, retest_id):
if request.method == 'POST':
retest.is_principal = True
retest.save(update_fields=['is_principal'])
email = EmailMessage('RMS', 'Your Notifications are Pending.', to=[.email])
email.send()
return render(request, 'retest/request.html' , {'retest': retest})
如何设置地址的条件?
答案 0 :(得分:0)
from django.core.mail import send_mail
def accepted(request, retest_id):
if request.method == 'POST':
retest.is_principal = True
retest.save(update_fields=['is_principal'])
users = [rep.user for rep in Rep.objects.filter(
batch=retest.batch, department=retest.dept
)]
for user in users:
user.email_user(subject='RMS', message='Your Notifications are Pending.')
return render(request, 'retest/request.html' , {'retest': retest})
答案 1 :(得分:0)
如果您确定您的代码100%完美且电子邮件无效。然后唱到你的邮件。然后试试。它会工作。如果您使用Gmail作为电子邮件主机,则Gmail会阻止此类操作,因为gmail不是为此而设计的。使用其他一些邮件服务,如弹性邮件或其他东西来正常工作。 Email in Django not working sometimes。你在这个qtn中也解决了同样的问题。