0

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?

toffee
  • 47
  • 2
  • 3
  • 9

2 Answers2

0
AddMessage 

takes a parameter m So in the app.route it should be changed to this

@app.route('/AddMessage/<m>')
DevHyperCoder
  • 895
  • 1
  • 9
  • 22
  • Could also be expecting it in the body of the post request, in which case the issue may be that `m` should not be a parameter at all and instead should be assigned as `request.data` after verifying the request was a POST. – zganger May 05 '20 at 03:16
  • That could be the case – DevHyperCoder May 05 '20 at 03:18
0

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):
   ...
aj3409
  • 186
  • 2
  • 14