English 中文(简体)
Flask - Cookies
  • 时间:2024-09-17

Flask – Cookies


Previous Page Next Page  

A cookie is stored on a cpent’s computer in the form of a text file. Its purpose is to remember and track data pertaining to a cpent’s usage for better visitor experience and site statistics.

A Request object contains a cookie’s attribute. It is a dictionary object of all the cookie variables and their corresponding values, a cpent has transmitted. In addition to it, a cookie also stores its expiry time, path and domain name of the site.

In Flask, cookies are set on response object. Use make_response() function to get response object from return value of a view function. After that, use the set_cookie() function of response object to store a cookie.

Reading back a cookie is easy. The get() method of request.cookies attribute is used to read a cookie.

In the following Flask apppcation, a simple form opens up as you visit ‘/’ URL.

@app.route( / )
def index():
   return render_template( index.html )

This HTML page contains one text input.

<html>
   <body>
      <form action = "/setcookie" method = "POST">
         <p><h3>Enter userID</h3></p>
         <p><input type =  text  name =  nm /></p>
         <p><input type =  submit  value =  Login /></p>
      </form>
   </body>
</html>

The Form is posted to ‘/setcookie’ URL. The associated view function sets a Cookie name userID and renders another page.

@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

‘readcookie.html’ contains a hyperpnk to another view function getcookie(), which reads back and displays the cookie value in browser.

@app.route( /getcookie )
def getcookie():
   name = request.cookies.get( userID )
   return  <h1>welcome  &plus;name&plus; </h1> 

Run the apppcation and visit http://localhost:5000/

ReadCookie HTML

The result of setting a cookie is displayed pke this −

Result of Setting Cookie

The output of read back cookie is shown below.

Reading Cookie Back Advertisements