request.form和request.args.get的区别
1、若get请求改为post请求,那么我们在后台使用request.args访问参数id的值,
使用request.form访问params参数集中各参数的值
例如:
前端代码:
<form method="post"> <tr> 昵称<input type="text" style="text-align:center" id="username" name="username" maxlength="8" {% if user_list %} value="{{ user_list.username }}" {% endif %}/> </tr> <tr> 真实名字<input type="text" style="text-align:center" id="realname" name="realname" maxlength="20" {% if user_list %} value="{{ user_list.realname }}" {% endif %}/> </tr> <tr>   </tr> <tr> <button type="submit" id="not_null" style="display: none">确定</button> <button type="button" onclick="judge_null()">确定</button> </tr> </form>
后端代码:
def edit_user(): if request.method == "GET": _id = request.args.get('id') user_list = json_util.loads( json_util.dumps(MongoHelpMaster('temp_users').database.find_one({'_id': ObjectId(_id)}))) return render_template('temp_site/peom/backend/edit_user.html', user_list=user_list, session=session["info"]) elif request.method == "POST": username = request.form['username'] realname = request.form['realname'] _id = request.args.get('id') print(_id) result = MongoHelpMaster('temp_users').database.update_one({"_id": ObjectId(_id)}, { "$set": {'username': username, 'realname': realname}})
当需要获取前端页面表单传过来的id值的时候,我们就需要用request.args.get,而不能用request.form