AttributeError:' NoneType'对象没有属性' app'当试图在Flask中发送邮件时

时间:2015-07-01 08:20:20

标签: python flask flask-mail

我正在按照烧瓶教程练习烧瓶邮件,但我遇到了一些看似错误的东西。我不明白会发生什么?

这是我的代码:

ServiceController

这是bug的信息:

def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    mail.send(msg)

1 个答案:

答案 0 :(得分:5)

当我调用with app.app_context():时,我已经解决了我的问题。

def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
                  sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    with app.app_context():
        msg.body = render_template(template + '.txt', **kwargs)
        msg.html = render_template(template + '.html', **kwargs)
        mail.send(msg)