Flask Cookies

A cookie is a kind of text file that is stored on a client's computer, whose purpose is to remember and track data related to the use of the client to improve the website based on the user experience and Statistics of the website.

To access cookies, you can use the cookie attribute. To set cookies, one can use the set_cookie() method of response objects. The requested object cookie attribute is a dictionary with all the cookies that the client transmits. If you want to use the sessions, do not use cookies directly, but use the sessions in the globally that add security to the cookies.

The Request object contains the cookie attribute. It is the object of the dictionary of all cookie variables and their corresponding values. It also includes the expiration time of itself. In Flask, cookies are set in the response object.

Example:- To set a cookie with the title 'coo' and the content 'bar' is given below.

from flask import *  
 app = Flask(__name__)  
 @app.route('/cookie')  
 def funcookie():  
 res = make_response("<h1>cookie is  set</h1>")  
 res.set_cookie('foo','bar')   
  return res  
  if __name__ == '__main__':  
  app.run(debug = True) 
Flask Cookies

If you wish you can also see the details of the cookies in your web-browser as shown below:-

Flask Cookies2

Login application in Flask

Now let's try to make a login application in the flask where a login page is shown to the user in which users have to fill some details like email address and password. If the details are correct, then the application will redirect the user to the success page where the message and a link to the profile are given. Otherwise, it will redirect the user to the error page Loginpage1.py.

Loginpage1.py

  From flask import *  
   app = Flask(__name__)  
   @app.route('/error')  
    def error():  
    return "<p><strong>Enter correct password</strong></p>"  
    @app.route('/')   
    def login():  
    return render_template("login.html") 
    @app.route('/success',methods = ['POST'])  
    def success():  
    if request.method == "POST":   
    email = request.form['email']  
    password = request.form['pass']  
    if password=="Erik_Akash":  
    resp = make_response(render_template('success.html'))  
    resp.set_cookie('email',email)  
    return resp   
    else:  
    return redirect(url_for('error'))  
    @app.route('/viewprofile')  
    def profile():   
     email = request.cookies.get('email')  
     resp = make_response(render_template('profile.html',name = email))  
     return resp    
    if __name__ == "__main__": 
    app.run(debug = True) 

Now run the file loginpage1.py, and this shows the login form as shown below:-

Flask Cookies3

Now click on the submit button. 

Flask Cookies5

As shown above the details filled by the user all are correct, and the user is successfully logged in.

Now click on the view profile button as shown below

Flask Cookies5

Cookies are set in the response objects. As it normally returns strings of display functions, Flask will turn them into response objects for you. If you want explicitly, you can use the make_response() function and modify it.