How to Create cookies in Django
Cookies are a day-to-day presence in the internet world. They do a great amount of work to make our surfing on the internet a lot easier. In this tutorial, we will learn what are cookies why are cookies used on the Internet. And also create cookies in Django in the server.
What are Cookies?
So, Cookies or HTTP Cookies are small pieces of information stored in your local browser on request from the server. They store users’ data for a finite amount of time or permanently and are maintained by the client browser.
How do Cookies Work?
Cookies work like any other HTTP requests on the internet. When the browser sends a request to the server it returns the response along with some cookies. These cookies may contain login information or other data, that helps the server to identify the user for the next requests.
When the next request is made to the server, the cookies generated previously are also passed along. This process is repeated every time. Until the cookie expires or the session has ended.
Django Implementation of Cookies
Now, we will look into the implementation of cookies in Django. Django reduces a lot of work otherwise required to create cookies. It has functions like set_cookie() and request.COOKIES are used to create cookies and get information from them.
The set_cookie() method is used to create cookies.
set_cookie(name, value, max_age)
The set_cookie() method has the following attributes:
- name: It specifies the name of the cookie.
- value: It specifies the data or value you want to store in the cookie.
- max_age: It is the time period of the cookie, after which it will expire. It is optional, if not present the cookie will last till the browser is closed.
The request.COOKIES is the method that returns the value present in the cookie. The syntax is request.COOKIES and the name of the cookie is passed in the squared parenthesis.
Here’s the code in views.py, two functions setcookie and getcookie are used to set and get cookies respectively.
from urllib import response from django.shortcuts import render, redirect, HttpResponse def index(request): return render(request, index.html) def setcookie(request): response=HttpResponse("Cookies has been set") response.set_cookie('cookie-tutorial','python') return response def getcookie(request): response=request.COOKIES['cookie-tutorial'] return HttpResponse('The set cookie is ' + response)
And the URLs specified in urls.py to access the function.
from django.contrib import admin from django.urls import path from home import views urlpatterns=[ path('admin/', admin.site.urls) path("", views.index, name="index") path("setcookie", views.setcookie, name="setcookie") path("getcookie", views.getcookie, name="getcookie") ]
Then, we run the Django server with,
python manage.py runsever
After starting the server, set cookie by running URL localhost:8000/setcookie. It shows the following out in the browser.
And view the cookie by using the URL localhost:8000/getcookie.
So, this is a basic tutorial on how to approach setting up cookies in Django. The retieval of user data can be implemented through this. You can take user login and pass it in the cookie value in setcookie, or can keep a count of the number of times the user has visited the website in a single session.
You can go through my other tutorials,
For more such tutorials, head over to CodeSpeedy