【问题标题】:encoding a form into an email body with python使用 python 将表单编码为电子邮件正文
【发布时间】:2020-02-10 22:26:18
【问题描述】:

我正在尝试在 python 中创建一个基本的 webapp,它允许通过表单和电子邮件询问信息我正在尝试将表单字段的内容编码到电子邮件正文中,但我不确定我做错了什么.我收到许多不同类型的错误,特别是当我尝试对内容进行 json 处理时出现以下错误:TypeError:ObjectId 类型的对象不是 JSON 可序列化的

    @app.route("/", methods = ['GET', 'POST'])
    def index():
        form = Init(request.form)
        if request.method == 'GET': # make sure the method used is define above
            return render_template('home.html', form = form), logging.warning("you are under the home page now using GET, well done bacco ")
        elif request.method == 'POST' and form.validate():
        # the following are the data from the init form
        name = form.name.data                           
        telefono = form.telefono.data
        email = form.email.data
        messaggio = form.messaggio.data

    # defining a new variable taking as input the values from the init form
        mymsg=[{ 
            "name": name, 
            "telefono": telefono, 
            "email" : email, 
            "messaggio" : messaggio
            }]
        # insert the list into the mongo db
        x = mycol.insert_many(mymsg), print("inserting this user: ", mymsg, "in the database called ", mycol)
        json_list = json.dumps(mymsg)
        msg = Message('New message from: ', sender='xxxxxxx@gmail.com', recipients=['xxxxxxx@gmail.com'], body = mymsg)
        msg.body = 'Messaggio: ', json_list
        mail.send(msg)
        return render_template('home.html', form = form), print("you are under the home page now using POST, data are sent to database")

【问题讨论】:

    标签: python forms email


    【解决方案1】:

    当调用insert_many 方法时,PyMongo 会自动将类型为 ObjectId_id 字段添加到您的所有文档中,即mymsg(请参阅此link 以供参考)。

    这就是json_list = json.dumps(mymsg) 给出错误的原因。

    处理BSON文档的json编码/解码,可以使用json_util

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2018-10-17
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2013-04-30
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多