DEV Community

KetanIP
KetanIP

Posted on • Updated on

πŸͺ Cookies in Flask 🍾

What are cookies ?

Cookies are a kind of storage, but a very small storage. It is mainly used to store small information such as csrf_token, login credentials ( encrypeted ), preferences, site statics and/or user activities. By using cookies one can provide an immersive experice to the users of the websites by making the website more personalized for them.

How to use "Cookies" in Flask ?

In flask using cookies is super simple. In flask cookies are Immutable Dictionary, we can also call as a dictionary. To view the value of the request.cookies attribute it will return an dictionary object. Let's see an understand it with an example,

app.py

from flask import Flask, request, make_response, redirect, url_for, render_template

app = Flask(__name__)

@app.route("/")
def home():
    userName = request.cookies.get("name")
    if userName:
        return f"Hello {userName}." 
    else:
        return redirect(url_for("setName"))
Enter fullscreen mode Exit fullscreen mode

Continue reading here πŸͺ Cookies in Flask 🍾.

Top comments (0)