【发布时间】: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")
【问题讨论】: