Flask Cookies
Flask Cookies
Cookies
- The cookies are stored in the form of text files on the client's machine. It is used to track the user's activities on the web.
- In flask, the cookies are associated with the Request object as the dictionary object of all the cookie variables. It is transmitted by the client.
response.setCookie(title, content, expiry time)
It is used to specify the expiry time, path, and the domain name of the website. set_cookie() - It is used to response the object. The response object can be formed by using the make_response() method in the view function.
Set Cookie
Additionally we can read the cookies stored on the client's machine using the get() method of the cookies attribute associated with the Request object.
request.cookies.get(<title>)
Read Also
Sample code
Output:
Flask Cookie
We can track the cookie details in the content settings of the browser.
Login application in Flask
- We will create a login application in the flask where a login page (login.html) is shown to the user which prompts to enter the email and password.
- If the password is "jtp", then the application will redirect the user to the success page (success.html) where the message and a link to the profile (profile.html) is given otherwise it will redirect the user to the error page.
List
Sample code
login.py
login.html
success.html
profile.html
Output
- Run the python script by using the command python login.py and visit localhost:5000/ on the browser.
Flask Login
- Click Submit. It will display the "Login Successful" message and provide a link to the profile.html
Flask Login Success
- Click on view profile. It will read the cookie set as a response from the browser and shows the following message.
Flask View Profile
If you want to learn about Python Course , you can refer the following links Python Training in Chennai , Machine Learning Training in Chennai , Data Science Training in Chennai , Artificial Intelligence Training in Chennai