3

I am working on a Flask project. After successful execution of login function, it should redirect on home page with some data. The data should be send by post. How can this be done??

Jatin Bhola
  • 661
  • 1
  • 6
  • 11
  • You can use ```redirect``` after success login. For send data look ```Response``` argument – julian salas Apr 21 '17 at 18:53
  • that I am doing. I cant figure out how to attach data to it. I have tried it with get request but dont know what to do for POST request. – Jatin Bhola Apr 21 '17 at 18:57
  • so, you should to use ```url_for("url", data=data)```, so in your other view use ```request.args['data']``` thus, should work fine, tell me please. – julian salas Apr 21 '17 at 19:18
  • it says `TypeError: redirect() got an unexpected keyword argument 'user_id'` when i gave the commad: `redirect(url_for('mainpage'),user_id=res['_id'])` – Jatin Bhola Apr 21 '17 at 19:27
  • it is giving 405 error code because it takes it as GET request – Jatin Bhola Apr 21 '17 at 19:32

1 Answers1

2

For send data after successful login you can use url_for within of redirect something as this:

@app.route('/login', methods = ['POST'])
def login():
    if request.method == 'POST' and request.form.get("username") == 'admin':
        return redirect(url_for('success',data=request.form.get("data")),code=307)
    else:
        return redirect(url_for('index'))

After success login you can use your data to send with data=data, so in your other view you get that data.

@app.route("/test/argument", methods=['POST'])
def success():
    messages = request.form.get('data')  # counterpart for url_for()
    return messages
julian salas
  • 3,714
  • 1
  • 19
  • 20