0

I am newbie in python and flask and trying to redirect a url with headers and unable to navigate, please provide me an example to solve the issue.

@app.route('/somepage')
def somepage():
   headers={'SomeName':'whatever'}
   return Response(redirect(url_for('home_page')),status=302, headers=headers)

@app.route('/home_page')
def home_page():
 if request.headers['SomeName'] == 'whatever':
    return render_template("home_page.html")
 else:
     return Response(status=405)

1 Answer 1

2

The flask url_for function can accept any number of **values you can use to send to the redirected view.

You can then use the Flask redirect function as so:

from flask import redirect
...
return redirect(url_for('home_page', headers=headers))

And then in the function use it as:

@app.route('/home_page')
def home_page():
 if json.loads(request.args.get("headers"))["SomeName"] == "whatever":
    return render_template("home_page.html")
 else:
     return Response(status=405)
Sign up to request clarification or add additional context in comments.

7 Comments

TypeError: redirect() got an unexpected keyword argument 'headers' i have received this error
redirected to the page but unable to render just saying Bad Request
please explain this error ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) at if request.args.get('headers')['SomeName'] == 'whatever':
SIngle quotes are not valid JSON, just use double quotes then.
Thank you for helping me, TypeError: string indices must be integers at if request.args.get("headers")["SomeName"] == "whatever":
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.