我用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'
我知道代码有问题,但是我不知道是什么。有什么主意吗?
AddMessage
采用参数m
因此,在app.route中,应将其更改为此
@app.route('/AddMessage/<m>')
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):
...