flask set and get cookies code example

Example 1: flask set cookie

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

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

Example 2: how to create cookies in flask

@app.route('/setcookie', methods = ['POST', 'GET'])def setcookie():   if request.method == 'POST':   user = request.form['nm']      resp = make_response(render_template('readcookie.html'))   resp.set_cookie('userID', user)      return resp