How to set cookie in Python Flask?

You have to return the response after setting the cookie.

@app.route("https://stackoverflow.com/")
def index():
    resp = make_response(render_template(...))
    resp.set_cookie('somecookiename', 'I am cookie')
    return resp 

This way a cookie will be generated in your browser, but you can get this cookie in the next request.

@app.route('/get-cookie/')
def get_cookie():
    username = request.cookies.get('somecookiename')

Leave a Comment