Managing Cookies with Python in Flask
Article have related YouTube video
Cookies are a fundamental part of web development, used to store small pieces of information on the client side. In this article, we will explore how to handle cookies in Flask, a popular Python web framework. We will cover the basics of creating and deleting cookies, as well as how to pass cookie data between the client and the server.
Before you even get started i would recommend to have a tool that can see all the cookies from the website that you are on. I use the extension to Google Chrome called Check my cookies feel free to use that, it is a helpful tool so you dont need to use javascript's console.log to see the cookies all the time.
The important things when creating cookies in Flask is to import the make_response. This enables you to create a response object where you can say both .set_cookie and .delete_cookie with a given name and when setting a cookie also i a value.
You need to return the response, else the cookie will nok be set or deleted.
Set the cookie by clicking a button
So to call this /set_cookie method, you can use an AJAX call. And send the key and value of the cookie to the route also, so you dont need to make a new route for every cookie you want to set.
Then call the endpoint by using AJAX: