get cookie flask code example
Example 1: flask cookckies
from flask import Flask, render_template, make_response, request
id_list = [0]
@app.route('/')
def setcookie():
resp = make_response(render_template('index.html'))
resp.set_cookie('id', id_list[len(id_list) - 1] + 1)
id_list.append(id_list[len(id_list) - 1] + 1)
return resp
@app.route('/show_id')
def show_id():
return f'<h1>welcome {request.cookies.get("id")} </h1>'
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