Django发送电子邮件不显示任何错误

时间:2018-11-26 15:15:02

标签: python django email

我正在制作一个带有“已付费”和“未付费”选项的停车系统,并且仅当选择了已付款表格时,我才希望系统发送一封带有付款确认的电子邮件。 我可以通过控制台发送电子邮件,但不能通过系统,也不会出现任何错误

models.py

from django.db import models
from django.core.mail import send_mail
import math

PAGO_CHOICES = (
('Não', 'Não Pago'),
('Sim', 'Pago')
)

class MovRotativo(models.Model):
    checkin = models.DateTimeField(auto_now=False, blank=False, null=False,)
    checkout = models.DateTimeField(auto_now=False, null=True, blank=True)
valor_hora = models.DecimalField(
    max_digits=5, decimal_places=2, null=False, blank=False)
veiculo = models.ForeignKey(
    Veiculo, on_delete=models.CASCADE, null=False, blank=False)
pago = models.CharField(max_length=15, choices=PAGO_CHOICES)

def horas_total(self):
    if self.checkout is None:
        return self.checkout == 0
    else:
        return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)

def total(self):
    return self.valor_hora * self.horas_total()

def __str__(self):
    return self.veiculo.placa

def send_email(self):
    if self.pago == 'Sim':
        send_mail(
            'Comprovante pagamento estacionamento',
            'Here is the message.',
            'estacioneaqui24@gmail.com',
            ['estacioneaqui24@gmail.com'],
            fail_silently=False,
        )

1 个答案:

答案 0 :(得分:0)

似乎您缺少信号或save()方法的替代。我将为您提供一个信号示例。我留下了一些印刷声明,以便您有更好的主意。

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=MovRotativo)
def before_movrotativo_save(sender, **kwargs):
    print(kwargs)
    instance = kwargs['instance']
    if instance.pago == 'Sim':
       print('Send email')
       instance.send_email()

这是我的示例,说明如何从shell进行测试:

python manage.py shell
>>> from application.models import MovRotativo
>>> from django.utils import timezone
>>> c = MovRotativo.objects.create(valor_hora=2, checkin=timezone.now())
>>> c.pago = 'Sim'
>>> c.save()