кодирование формы в тело письма с помощью python - PullRequest
0 голосов
/ 14 октября 2019

Я пытаюсь создать базовое веб-приложение на python, которое позволяет запрашивать информацию через форму и электронную почту. Я пытаюсь зашифровать содержимое полей формы в теле письма, но я не уверен, что я делаю неправильно,Я получаю много разных типов ошибок, в частности, следующие, когда я пытаюсь json контент: TypeError: Объект типа ObjectId не является JSON serializable

    @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")

1 Ответ

0 голосов
/ 14 октября 2019

Когда вызывается метод insert_many, PyMongo автоматически добавляет поле _id, набранное ObjectId , во все ваши документы, т.е. mymsg (см. Ссылку для справки). ).

Вот почему json_list = json.dumps(mymsg) выдает ошибку.

Для обработки json-кодирования / декодирования документов BSON вы можете использовать json_util .

Надеюсь, чтопомогает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...