Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将django loader.render_to_string()转换为sendgrid的flask_Python_Django_Flask_Sendgrid - Fatal编程技术网

Python 将django loader.render_to_string()转换为sendgrid的flask

Python 将django loader.render_to_string()转换为sendgrid的flask,python,django,flask,sendgrid,Python,Django,Flask,Sendgrid,我们正在将django应用程序转换为flask,但我找不到类似于flask的django loader.render_to_字符串 有什么建议我在烧瓶中尝试了渲染模板字符串,但没有成功 from django.template import loader from sendgrid import SendGridAPIClient def send_email: dt = { 'name': xxxxxx, 'application_name': xxx

我们正在将django应用程序转换为flask,但我找不到类似于flask的django loader.render_to_字符串

有什么建议我在烧瓶中尝试了渲染模板字符串,但没有成功

from django.template import loader
from sendgrid import SendGridAPIClient

def send_email:
    dt = {
        'name': xxxxxx,
        'application_name': xxxxxx,
    }

    email_template_text = loader.render_to_string("xxxxx.txt", dt)
    email_template_html = loader.render_to_string("xxxxx.html", dt)

    data = {
        "personalizations": [
            {
                "to": [
                    {
                        "email": xxxxx
                    }
                ],
                "subject": xxxxxx
            }
        ],
        "from": {
            "email": xxxxxxx
        },
        "content": [
            {
                "type": "text/plain",
                "value":  email_template_text
            },
            {
                "type": "text/html",
                "value": email_template_html
            }
        ]
    }
    sg = SendGridAPIClient(xxxxx)
    response = sg.send(data)

flask.render_模板
[]应该满足您期望的结果

记住打开模板上下文参数的字典

此外,这必须在flask应用程序的上下文中完成。 如果这是一个可能由工作人员运行的代码,则代码将类似于:

从flask导入渲染模板,flask
app=烧瓶(名称)
使用app.app_context():
email\u template\u text=render\u template(“xxxxx.txt”,**dt)
email\u template\u html=render\u template(“xxxxx.html”,**dt)