我们如何使用 Flask 的 request.form 从用户那里获取 json 格式的数据?

问题描述 投票:0回答:1

我在做一个用户管理系统应用,一个用户可以有多个admin,所以我在mysql中有一个列是json格式的,但是这个列怎么更新,怎么添加呢?当我尝试使用从用户那里获得的数据更新 request.form 时,出现错误“400 错误请求:浏览器(或代理)发送了该服务器无法理解的请求”。

@Admin.route('/update', methods = ['POST' , 'GET'])
def update():
        # GET : The most common method. A GET message is send, and the server returns data
        # POST : Used to send HTML form data to the server. The data received by the POST method is not cached by the server.
    if request.method == 'POST':
        # get data from user
        id = request.form['userid']
        name = request.form['name']
        email = request.form['email']
        role = request.form['role']
        managerid = request.form['managerid']
        managedBY = request.form['managedBY']

        try:
            connection = create_connection()
            cursor = connection.cursor()
            # After connecting to the database, the following query has been performed to update the data with the data from the user
            sql = "UPDATE users SET name= %s, email= %s,  role=%s, managerid=%s, managedBY=%s WHERE userid= %s"
            values = (name , email,  role, managerid, managedBY, id)
            cursor.execute(sql , values)
            connection.commit()
            flash("Data Updated Succesfully")
        except pymysql.connect.Error as err:
            print('hata: ', err)

        finally:
            connection.close()
            print('database bağlantısı kapandı.')

        return redirect(url_for('Admin.Index'))[database column](https://i.stack.imgur.com/19IM8.png)```


Since a user has more than one manager, I had to use json data in mysql, but I cannot connect this json column with form operations in html. My goal is to reach this column and make changes in it (update, insert, delete)
python mysql flask mysql-json flask-mysql
1个回答
0
投票

建议您遵循数据库最佳实践,在这种情况下,建议使用一对多关系一对多,因为用户将拥有多个经理。这将使您的应用程序健壮并消除冗余。

© www.soinside.com 2019 - 2024. All rights reserved.