Python flask POST请求

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

我用Python开发了一个应用,并使用了flask。这是一段代码片段,试图通过用户输入生成一条消息,然后将其附加到我的数据库中:

@app.route('/MakeMessage',methods = ['POST', 'GET'])
def MakeMessage():
   if request.method == 'POST':
       user_id = request.form['user_id']
       content = request.form['content']
       paticipants = [request.form['participant1'],request.form['participant2'],request.form['participant3']]
       m = Message(user_id=user_id,content=content,participants=paticipants)
       return redirect('/AddMessage',m = m)

@app.route('/AddMessage',methods = ['POST', 'GET'])
def AddMessage(m):
   if request.method == 'POST':
      db.session.add(m)
      db.session.commit()
      return 'Your message has been successfully saved'

我知道代码有问题,但是我不知道是什么。有什么主意吗?

python http flask post
2个回答
0
投票
AddMessage 

采用参数m因此,在app.route中,应将其更改为此

@app.route('/AddMessage/<m>')

0
投票
from flask import redirect, url_for, request

@app.route('/MakeMessage',methods = ['POST', 'GET'])
def MakeMessage():
   if request.method == 'POST':
      ...
      return redirect(url_for('/AddMessage',m=m))

@app.route('/AddMessage/<m>',methods = ['POST', 'GET'])
def AddMessage(m):
   ...
© www.soinside.com 2019 - 2024. All rights reserved.