

{"id":55207,"date":"2019-04-26T19:03:59","date_gmt":"2019-04-26T13:33:59","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=55207"},"modified":"2021-06-21T12:19:31","modified_gmt":"2021-06-21T06:49:31","slug":"django-cookies-handling","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/","title":{"rendered":"Django Cookies and Cookies Handling &#8211; How to Create Cookies in Django"},"content":{"rendered":"<p>When we see the word cookies, we expect a chocolate chip baked biscuit, but here we will talk about computer cookies. They do a great piece of work that makes it more easy for you to surf the Internet but they can be irritating if you do not know how to remove or delete cookies.<\/p>\n<p>This tutorial will help you understand the basic concept of computer cookies and why are they used on the web. Also, we will learn to create Django cookies with the server.<\/p>\n<p>Firstly, let&#8217;s see the definition of cookies.<\/p>\n<h3>What are Cookies?<\/h3>\n<p><em>Cookies, technically called HTTP Cookies are small text files which are created and maintained by your browser on the particular request of Web-Server<\/em>. They are stored locally by your browser, and most browser will also show you the cookies generated in the Privacy and Security settings of the browser.<\/p>\n<p>HTTP is a stateless protocol. When any request is sent to the server, over this protocol, the server cannot distinguish whether the user is new or has visited the site previously.<\/p>\n<p>Suppose, you are logging in any website, that website will respond the browser with some cookies which will have some unique identification of user generated by the server and some more details according to the context of the website.<\/p>\n<p>Cookies made these implementations possible with ease which were previously not possible over HTTP implementation.<\/p>\n<h3>How do Cookies work?<\/h3>\n<p>Cookies work like other HTTP requests over the Internet. In a typical web-system, the browser makes a request to the server. The server then sends the response along with some cookies, which may contain some login information or some other data.<\/p>\n<p>When the browser makes a new request, the cookie generated previously is also transmitted to the server. This process is repeated every time a new request is made by the browser.<\/p>\n<p>The browser repeats the process until the cookie expires or the session is closed and the cookie is deleted by the browser itself.<\/p>\n<p>Then, the cookie applies in all sorts of tasks, like when your login to a website or when shopping online on the web. Google AdSense and <strong>Google Analytics<\/strong> can also track you using the cookies they generate. Different websites use cookies differently according to their needs.<\/p>\n<p>Now, let&#8217;s move on to the core section in which you will learn to create cookies in the Django framework.<\/p>\n<h3>Creating Cookies in Django<\/h3>\n<p>Django bypasses lots of work which otherwise would be required when working on cookies. Django has methods like set_cookie() which we can use to create cookies very easily.<\/p>\n<p>The set_cookie() has these attributes:<\/p>\n<h4><strong>name<\/strong>: It specifies the name of cookie.<\/h4>\n<h4><strong>value:<\/strong>\u00a0It specifies the text or variable you want to store in the cookie.<\/h4>\n<p><strong>max_age:<\/strong>\u00a0It is the time period of cookie in seconds. After the time period completes, it will expire. It is an optional parameter; if not present then the cookie will exist till the time browser close.<\/p>\n<p>We will add this method to our view functions for creating cookies.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def setcookie(request):\r\n    html = HttpResponse(\"&lt;h1&gt;Dataflair Django Tutorial&lt;\/h1&gt;\")\r\n    html.set_cookie('dataflair', 'Hello this is your Cookies', max_age = None)\r\n    return html<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55225\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output.png\" alt=\"set cookie output\" width=\"1046\" height=\"135\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output.png 1046w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output-150x19.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output-300x39.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output-768x99.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output-1024x132.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/set-cookie-output-520x67.png 520w\" sizes=\"auto, (max-width: 1046px) 100vw, 1046px\" \/><\/a><\/p>\n<p>This function will return to the browser, and when you run it, you can check the created cookie in your browser settings.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55226\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data.png\" alt=\"localhost in manage cookies and site data\" width=\"893\" height=\"327\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data.png 893w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data-150x55.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data-300x110.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data-768x281.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/localhost-in-manage-cookies-and-site-data-520x190.png 520w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/a><\/p>\n<p>The cookie we just created can be seen here. Since we are using localhost, therefore, the site name comes as localhost.<\/p>\n<p>Now, add these paths to urls.py of your project.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">    path('setcookie', setcookie),\r\n    path('getcookie', showcookie),<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/path-site-cookie-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55227\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/path-site-cookie-output.png\" alt=\"path site cookie output\" width=\"509\" height=\"74\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/path-site-cookie-output.png 509w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/path-site-cookie-output-150x22.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/path-site-cookie-output-300x44.png 300w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><\/a><\/p>\n<p>It will add the necessary paths to call view functions. We will define both the functions in the coming section.<\/p>\n<p>The output of the setcookie() view function will be this.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55228\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output.png\" alt=\"Dataflair Django Tutorial site output\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Dataflair-Django-Tutorial-site-output-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<h3>Read Cookies from request<\/h3>\n<p>Cookies pass to the particular website every time the client makes a request. Therefore, server every time receives a cookie alongside the request. Django makes it very easy for us to retrieve data from a cookie.<\/p>\n<h4>1. Using request.COOKIES[]<\/h4>\n<p>Django\u2019s request object has an attribute COOKIES, like COOKIES array in PHP.<\/p>\n<p>COOKIES is a special attribute of request, and its value is the name of the cookie from which you want to read data. Since there can be multiple cookies, you can change this value many times according to the type of cookie you want to store.<\/p>\n<p>Here is how you can use the same.<\/p>\n<p>We will use basic syntax to access a class parameter as:<\/p>\n<p>request.COOKIES[\u2018cookie_name\u2019]<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def showcookie(request):\r\n    show = request.COOKIES['dataflair']\r\n    html = \"&lt;center&gt; New Page &lt;br&gt;{0}&lt;\/center&gt;\".format(show)\r\n    return HttpResponse(html)<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55229\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output.png\" alt=\"show cookie output\" width=\"812\" height=\"130\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output.png 812w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output-150x24.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output-300x48.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output-768x123.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/show-cookie-output-520x83.png 520w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/a><\/p>\n<p>This code will read the cookie named dataflair, from the current request received by the browser. Also, we can again generate a new cookie in this function if the values changes.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55241\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site.png\" alt=\"Cookies highlighted on site\" width=\"1600\" height=\"519\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site-150x49.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site-300x97.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site-768x249.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site-1024x332.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Cookies-highlighted-on-site-520x169.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>2. Using request.COOKIES.get()<\/p>\n<p>Django also provides a method to get the desired value from the cookie. You can directly access that value using get method over request object.<\/p>\n<p>The syntax that we will request :<\/p>\n<p>COOKIES.get(\u2018cookie_name\u2019, \u2018value\u2019)<\/p>\n<p>Let\u2019s make some changes in our previous example.<\/p>\n<p>In our views.py write this code<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.shortcuts import render, redirect\r\nfrom django.http import HttpResponse<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55231\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output.png\" alt=\"django shortcuts import output\" width=\"768\" height=\"82\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output-150x16.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output-300x32.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/django-shortcuts-import-output-520x56.png 520w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/p>\n<p>We will need to apply redirect(), so import it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def setcookie(request):\r\n    html = HttpResponse(\"&lt;h1&gt;Dataflair Django Tutorial&lt;\/h1&gt;\")\r\n    if request.COOKIES.get('visits'):\r\n        html.set_cookie('dataflair', 'Welcome Back')\r\n        value = int(request.COOKIES.get('visits'))\r\n        html.set_cookie('visits', value + 1)\r\n    else:\r\n        value = 1\r\n        text = \"Welcome for the first time\"\r\n        html.set_cookie('visits', value)\r\n        html.set_cookie('dataflair', text)\r\n    return html<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55233\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output.png\" alt=\"Welcome for the first time output\" width=\"1041\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output.png 1041w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output-150x61.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output-300x121.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output-768x310.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output-1024x413.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Welcome-for-the-first-time-output-520x210.png 520w\" sizes=\"auto, (max-width: 1041px) 100vw, 1041px\" \/><\/a><\/p>\n<p>This code will check whether the request has cookies or not, it will then set the cookies according to that.<\/p>\n<p>We will use the COOKIES.get() to achieve that.<\/p>\n<p>Now, make the\u00a0change to our showcookie()<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def showcookie(request):\r\n    if request.COOKIES.get('visits') is not None:\r\n        value = request.COOKIES.get('visits')\r\n        text = request.COOKIES.get('dataflair')\r\n        html = HttpResponse(\"&lt;center&gt;&lt;h1&gt;{0}&lt;br&gt;You have requested this page {1} times&lt;\/h1&gt;&lt;\/center&gt;\".format(text, value))\r\n        html.set_cookie('visits', int(value) + 1)\r\n        return html\r\n    else:\r\n        return redirect('\/setcookie')<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55232\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output.png\" alt=\"cookies get output\" width=\"1290\" height=\"220\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output.png 1290w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output-150x26.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output-300x51.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output-768x131.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output-1024x175.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/cookies-get-output-520x89.png 520w\" sizes=\"auto, (max-width: 1290px) 100vw, 1290px\" \/><\/a><\/p>\n<p>This code will check for the cookies, and now we can run the code.<\/p>\n<p>Just start your server and\u00a0type getcookie\/ in the URL bar.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55234\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1.png\" alt=\"Website highlighted on site\" width=\"1477\" height=\"875\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1.png 1477w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1-150x89.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1-300x178.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1-768x455.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1-1024x607.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Website-highlighted-on-site-1-520x308.png 520w\" sizes=\"auto, (max-width: 1477px) 100vw, 1477px\" \/><\/a><\/p>\n<p>You should see an output like this. Then, the code checks whether the cookie has a value or not and if it doesn\u2019t find anything it will redirect you to the setcookie.<\/p>\n<p>Now again search for getcookie\/ in URL bar.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55236\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time.png\" alt=\"localhost requested page 1 time\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/requested-page-1-time-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>You shall see an output like this. Again, try reloading it a couple of times.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55237\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times.png\" alt=\"localhost Requested page 11 times\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-11-times-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>We can still change this code. You will have to visit the setcookie\/ url to change the text again.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55238\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times.png\" alt=\"localhost Requested page 27 times\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Requested-page-27-times-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<h3>Deleting a Cookie in Django<\/h3>\n<p>Django provides you with easy methods for deleting cookies. As we used set_cookie() to create cookies, we can also delete cookies using a similar function, delete_cookie()<\/p>\n<p>The delete_cookie() takes in the name of the cookie to be deleted, and this method is associated with the response object.<\/p>\n<p>Add this function to your views.py file<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def delete_co(request):\r\n    if request.COOKIES.get('visits'):\r\n       response = HttpResponse(\"&lt;h1&gt;dataflair&lt;br&gt;Cookie deleted&lt;\/h1&gt;\")\r\n       response.delete_cookie(\"visits\")\r\n    else:\r\n        response = HttpResponse(\"&lt;h1&gt;dataflair&lt;\/h1&gt;need to create cookie before deleting\")\r\n    return response\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55239\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output.png\" alt=\"delete cookie output\" width=\"1313\" height=\"259\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output.png 1313w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output-150x30.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output-300x59.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output-768x151.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output-1024x202.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/delete-cookie-output-520x103.png 520w\" sizes=\"auto, (max-width: 1313px) 100vw, 1313px\" \/><\/a><\/p>\n<p>This should generate output:<\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55240\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output.png\" alt=\"cookie deleted output\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/dataflair-cookie-deleted-output-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a>Summary<\/h3>\n<p>Django is very fun to play with, isn\u2019t it? Well, that\u2019s why we like Django and also, it\u2019s quite a great piece of technology. Make some codes and edit this one to be familiar with the topics we have covered until this tutorial.<\/p>\n<p>This tutorial covered most of the basics of cookies and how you can use them on Django. Using some right level of Python, now you are ready to make your cookies and use them as much as you want.<\/p>\n<p>But as a developer, you will need more knowledge of the norms and laws when you are using cookies on a hosted server like the ones where the website asks for your permission for accepting cookies.<\/p>\n<p>Any doubts or feedbacks? Feel free to enter in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we see the word cookies, we expect a chocolate chip baked biscuit, but here we will talk about computer cookies. They do a great piece of work that makes it more easy for&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":55258,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[19620,19619,19621,19622],"class_list":["post-55207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-creating-django-cookies","tag-django-cookies","tag-what-are-cookies","tag-working-of-cookies"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django Cookies and Cookies Handling - How to Create Cookies in Django - DataFlair<\/title>\n<meta name=\"description\" content=\"Django cookies tutorial covers the explanation of cookies, working of cookies, the process to create cookies with Django server and deleting the cookies.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django Cookies and Cookies Handling - How to Create Cookies in Django - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Django cookies tutorial covers the explanation of cookies, working of cookies, the process to create cookies with Django server and deleting the cookies.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-26T13:33:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django Cookies and Cookies Handling - How to Create Cookies in Django - DataFlair","description":"Django cookies tutorial covers the explanation of cookies, working of cookies, the process to create cookies with Django server and deleting the cookies.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/","og_locale":"en_US","og_type":"article","og_title":"Django Cookies and Cookies Handling - How to Create Cookies in Django - DataFlair","og_description":"Django cookies tutorial covers the explanation of cookies, working of cookies, the process to create cookies with Django server and deleting the cookies.","og_url":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-04-26T13:33:59+00:00","article_modified_time":"2021-06-21T06:49:31+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Django Cookies and Cookies Handling &#8211; How to Create Cookies in Django","datePublished":"2019-04-26T13:33:59+00:00","dateModified":"2021-06-21T06:49:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/"},"wordCount":1171,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg","keywords":["Creating Django Cookies","Django Cookies","What are Cookies","Working of Cookies"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/","url":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/","name":"Django Cookies and Cookies Handling - How to Create Cookies in Django - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg","datePublished":"2019-04-26T13:33:59+00:00","dateModified":"2021-06-21T06:49:31+00:00","description":"Django cookies tutorial covers the explanation of cookies, working of cookies, the process to create cookies with Django server and deleting the cookies.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-cookies-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/04\/Django-Cookies-Cookies-Handling.jpg","width":802,"height":420,"caption":"Django Cookies & Cookies Handling"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-cookies-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Django Cookies and Cookies Handling &#8211; How to Create Cookies in Django"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/55207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=55207"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/55207\/revisions"}],"predecessor-version":[{"id":97684,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/55207\/revisions\/97684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/55258"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=55207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=55207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=55207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}