Search code examples
pythonflaskcookies

how to set a cookie in a flask project?


i want to set a cookie in my flask project, I don't want to use the way below, I just want a cookie to be set for the user when he enters a route.

res = make_response('...')
res.set_cookie("login", value=password)

I tried this way but it didn't work

res = make_response()
res.set_cookie("login", value=password)

Solution

  • If I am understanding this correctly, you can just do this:

    @app.route('/')
    def index():
        # Set the cookie
        response = make_response('')
        response.set_cookie('login', 'password')
        return response
    

    This way, the default behavior when the user accesses the route '/' will be to set this cookie

    More information here: https://pythonbasics.org/flask-cookies/