I develop an app in Python and use flask. Here is a snippet of code that tries to generate a message by user input and then attach it to my database:
@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'
I know something is wrong with the code, but I don't know what. Any idea?
AddMessage
takes a parameter m
So in the app.route it should be changed to this
@app.route('/AddMessage/<m>')
You would want to use url_for()
in the redirect function to redirect to a route in your application. In addition, you need to put <m>
in your route for AddMessage
.
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):
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.