小编Ahm*_*dir的帖子

运行时错误:在应用程序上下文之外工作。烧瓶 - 邮件

from flask import current_app as app
from flask import render_template
from threading import Thread
from flask_mail import Message



def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject,
                  sender=app.config['MAIL_SENDER'], recipients=[to])
    # msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    thr = Thread(target=send_async_email, args=(app,msg))
    thr.start()
    return thr
    #mail.send(msg)
Run Code Online (Sandbox Code Playgroud)

运行时错误:在应用程序上下文之外工作。

这通常意味着您尝试使用需要以某种方式与当前应用程序对象交互的功能。要解决此问题,请使用 app.app_context() 设置应用程序上下文。请参阅文档以获取更多信息。

我以为我已经创建了 app_context,但代码仍然显示运行时错误。请帮忙,谢谢。

python flask python-3.x flask-mail

2
推荐指数
1
解决办法
5690
查看次数

标签 统计

flask ×1

flask-mail ×1

python ×1

python-3.x ×1