django cookies

Django Cookies

Django cookies are a small piece of information that is stored in the client browser. It is used to store users’ information in a file permanently or a specific period of time. So the cookie will automatically remove when gets expire. In this tutorial, you will learn about how to set and get cookies in Django web applications.

There are different browsers are interact with a web server at the same time, it needs to identify which browser a specific request comes from. To identify the web browser web servers use cookies. In our words, a cookie is a text file with a small piece of data that the web server sends to a web browser.

Here Django allows you to set, get and delete cookies, let’s go with an example.

Setting a cookie

Syntax:

set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=False, httponly=False, samesite=None)
  • key is the cookie name
  • value represents the cookie value
  • max_age is the timedelta object or an integer value that represents the no of seconds that specifies how long the cookie should expire and the defalut value is None
  • expires a datetime object in UTC or a string in the format or a string in the format "Wdy, DD-Mon-YY HH:MM:SS GMT"
  • secure=True when you want the web browser to send the cookie to the server side is request is HTTPS only.
  • httponly make it True if you don’t want the client-side javascript to access the cookie
  • Set samesite=’None’ to allow the cookie to be sent all the time when cross-site requests.

Example:

def setcookie(self, request):
        response = HttpResponse("Cookie Set")
        response.set_cookie("progamspeaker-", "www.programspeaker.com")
        return response

Getting a cookie

Syntax:

request.COOKIES['cookie_key']

Example:

def get(self, request):
        website = request.COOKIES["progamspeaker-"]
        return HttpResponse("progamspeaker- " + website)

Here we get the cookie value using the “programspeaker-” key. If the key does’t exist Django will through an error, so avoid it by using get() method. Then it will return None value.

 request.COOKIES.get("progamspeaker-")

Deleting a cookie

To delete a cookie we can use delete_cookie() method.

Syntax:

delete_cookie(key, path='/', domain=None, samesite=None)

Example:

def get(self, request):
        response = HttpResponse("Cookie deleted")
        response.delete_cookie("progamspeaker-")
        return response

The delete_cookie() method deletes a cookie with a specified name. And it fails if the key doesn’t exist. Note that the path and domain must have the same values as you used in the set_cookie() method.

At the end, a cookie is a piece of data that the web server sends the data to the web browser and the browser may store it or not. The web browser sends the cookie back to the web server in the subsequent requests in the header of the HTTP request. set_cookie() is used to create a cookie and request.COOKIES is used for get and also delete_cookie() help to remove it.

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top