Login Application in Flask using cookies
Letâs develop a simple login page with Flask using cookies. First, we are creating the main python file â app.py in our code editor. Next, we will create the UI of our web page which is Login.html where the user can enter his username and password. In this app.py, we are storing the username as cookie to know which user logged in to the website. In the below code, we are requesting the stored cookie from the browser and displaying it on the next page which routes to user details page.
app.py
Python3
from flask import Flask, request, make_response, render_template app = Flask(__name__) @app .route( '/' , methods = [ 'GET' ]) def Login(): return render_template( 'Login.html' ) @app .route( '/details' , methods = [ 'GET' , 'POST' ]) def login(): if request.method = = 'POST' : name = request.form[ 'username' ] output = 'Hi, Welcome ' + name + '' resp = make_response(output) resp.set_cookie( 'username' , name) return resp app.run(debug = True ) |
Login.html
HTML
<!DOCTYPE html> < html > < head > < title >Login</ title > </ head > < body > < form method = "post" action = "/details" > < label for = "username" >Username</ label > < input type = "text" name = "username" id = "username" /> < br /> < br > < label for = "password" >Password</ label > < input type = "password" name = "password" id = "password" /> < br /> < br > < input type = "submit" name = "submit" id = "submit" value = "Login" /> </ form > </ body > </ html > |
Output:
From the above image, the user can also see the cookies of a website. Here, âusernameâ is the key and its value is âGreeshmaâ which reminds us that cookies are generally key-value pairs. To see the cookies in your browser, click the last 3 dots on the browserâs right corner>> More Tools>>Developer Tools>>Application window.
Flask Cookies
Flask is a lightweight, web development framework built using python language. Generally, for building websites we use HTML, CSS and JavaScript but in flask the python scripting language is used for developing the web-applications.
To know more about Flask and how to run an application in Flask: Flask â First Application Creation
What are Cookies?
Technically, cookies track user activity to save user information in the browser as key-value pairs, which can then be accessed whenever necessary by the developers to make a website easier to use. These enhances the personal user experience on a particular website by remembering your logins, your preferences and much more.
For running a flask application, we need to have some prerequisites like installing the flask.
Prerequisites:
Use the upgraded version of pip by below command in your terminal. In this article, I am using Visual Studio Code to run my flask applications.
Python -m pip install âupgrade pip Python -m pip install flask
Setting Cookies in Flask:
set_cookie( ) method: Using this method we can generate cookies in any application code. The syntax for this cookies setting method:
Response.set_cookie(key, value = '', max_age = None, expires = None, path = '/', domain = None, secure = None, httponly = False)
Parameters:
- key â Name of the cookie to be set.
- value â Value of the cookie to be set.
- max_age â should be a few seconds, None (default) if the cookie should last as long as the clientâs browser session.
- expires â should be a datetime object or UNIX timestamp.
- domain â To set a cross-domain cookie.
- path â limits the cookie to given path, (default) it will span the whole domain.
Example:
Python3
from flask import Flask, request, make_response app = Flask(__name__) # Using set_cookie( ) method to set the key-value pairs below. @app .route( '/setcookie' ) def setcookie(): # Initializing response object resp = make_response( 'Setting the cookie' ) resp.set_cookie( 'GFG' , 'ComputerScience Portal' ) return resp app.run() |
Running the code in Visual Studio Code application.
Output: Go to the above-mentioned url in the terminal -For Example â http://127.0.0.1:5000/route-name. Here the route-name is setcookie.
Getting Cookies in Flask:
cookies.get( )
This get( ) method retrieves the cookie value stored from the userâs web browser through the request object.
Python3
from flask import Flask, request, make_response app = Flask(__name__) # getting cookie from the previous set_cookie code @app .route( '/getcookie' ) def getcookie(): GFG = request.cookies.get( 'GFG' ) return 'GFG is a ' + GFG app.run() |
Output:
Contact Us