{"id":83711,"date":"2021-08-13T09:00:44","date_gmt":"2021-08-13T03:30:44","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=83711"},"modified":"2021-08-13T09:00:44","modified_gmt":"2021-08-13T03:30:44","slug":"django-cookies-handling","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/","title":{"rendered":"Django Cookies Handling- Learn the Way to Set up Cookies in Django"},"content":{"rendered":"<p>Django is a framework that allows us to interact with cookies. Cookies give you the possibility of storing and retrieving data that is saved in sessions. These cookies have an expiration date and are deleted after a specified amount of time has passed.<\/p>\n<p>We already know that whenever we log in to any web page or application, the site will ask for the storage of our user id and password, as well as the auto-filling of a few details based on our previous logged in sessions. This is all done using cookies. Similarly, we can store cookies on the client-side to assist end-users in making their jobs easier.<\/p>\n<p>This tutorial will teach you the basics of computer cookies and why they are used on the internet. We&#8217;ll also learn how to use the server to create Django cookies.<\/p>\n<p>Let&#8217;s start with a definition of cookies.<\/p>\n<h3>What are Cookies in Django?<\/h3>\n<p>Cookies, also known as HTTP Cookies, are little text files that your browser creates and maintains in response to a specific Web-Server request. Your browser saves them locally, and most browsers will display you the cookies that have been generated under the Privacy and Security settings.<\/p>\n<p>The HTTP protocol is stateless. When a request is sent to a server using this protocol, the server is unable to tell whether the user is new or has previously visited the site.<\/p>\n<p>If you log in to a website, the website will send cookies to your browser that contain a unique user identity issued by the server as well as other information relevant to the website&#8217;s context.<\/p>\n<p>Cookies make it simple to incorporate certain features that were previously impossible to do using HTTP.<\/p>\n<h3>How do Django Cookies work?<\/h3>\n<p>Cookies function in the same way as other HTTP requests on the Internet.<\/p>\n<ul>\n<li>The request is sent to the server by the browser.<\/li>\n<li>The server transmits the response to the browser along with one or more cookies.<\/li>\n<li>The cookie that the browser receives from the server is saved. From now on, every time a request is made to the server, the browser will send this cookie to the server until the cookie expires.<\/li>\n<li>The cookie is removed from the browser when it expires.<\/li>\n<\/ul>\n<p>Cookies are used in a variety of contexts, such as when you log in to a website or shop online. The cookies that Google AdSense and Google Analytics create can also be used to track you. Cookies are used differently by different websites depending on their needs.<\/p>\n<h3>What is the purpose of cookies in Django?<\/h3>\n<p>You may have noticed that if you leave an eCommerce or social networking site like Facebook without signing out, your account remains signed in the next time you visit the site. Cookies are used to accomplish this (which contain user-session information).<\/p>\n<p>Similarly, you can see product recommendations on several eCommerce websites. This is due to the cookies that store the search results on your browser.<\/p>\n<h3>How do I use Django to set cookies?<\/h3>\n<h4>Cookie Attributes in Django<\/h4>\n<p>A cookie attribute in Django can do one of two things. It can place (set) a cookie on a user&#8217;s computer and then access those cookies (get). Let\u2019s understand both the concepts, one by one.<\/p>\n<h5>1. Set cookies in Django<\/h5>\n<p>This cookie attribute creates a cookie, which the server sends to the user&#8217;s browser to save data.<\/p>\n<p>set cookie() has the following syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">set_cookie(cookie_name, value, max_age = None, expires = None)<\/pre>\n<p><strong>name:<\/strong> It provides the cookie name.<\/p>\n<p><strong>Value:<\/strong> The text or variable you wish to store in the cookie is specified by value.<\/p>\n<p><strong>max_age:<\/strong> It&#8217;s the duration of a cookie in seconds. It will expire at the end of the period. It&#8217;s an optional parameter; if it&#8217;s not set, the cookie will remain active until the browser is closed.<\/p>\n<p><strong>Expires:<\/strong> A string in the format &#8220;Wdy, DD-Mon-YY HH:MM:SS GMT&#8221; or a datetime.datetime object in UTC should be used. The max_age will be determined if expires is a datetime object.<\/p>\n<h5>2. Get cookies in Django<\/h5>\n<p>The server uses this feature to retrieve previously delivered cookies and read data from them. To obtain the cookie, use the following syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">request.COOKIES['cookie_Name']<\/pre>\n<p>Let&#8217;s look at an example of how to manually set a cookie in Django:<\/p>\n<h4>Coding the views<\/h4>\n<p>We recently learned what cookies are. The following steps will help you to make your first cookie. The foremost step is to modify the view.py file.<\/p>\n<h5>View for sending a cookie<\/h5>\n<p>In your views.py file, include the following code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def setcookie(request):\n    html = HttpResponse(\"&lt;h1&gt;Welcome to TechVidvan Employee Portal&lt;\/h1&gt;\")\n    html.set_cookie('TechVidvan', 'We are setting a cookie', max_age = None)\n    return html\n<\/pre>\n<h5>Setting up the urls.py file<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">path('setcookie', views.setcookie),<\/pre>\n<h5>View for getting Cookie<\/h5>\n<p>In your views.py file, include the following code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def showcookie(request):\n    show = request.COOKIES['TechVidvan']\n    html = \"&lt;center&gt; New Page &lt;br&gt;{0}&lt;\/center&gt;\".format(show)\n    return HttpResponse(html)<\/pre>\n<h5>Setting up the urls.py file<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">path('getcookie', views.showcookie),<\/pre>\n<h4>Django Cookie implementation<\/h4>\n<p>Now let\u2019s see the combined Views.py file (which includes both of the aforementioned section files).<\/p>\n<p>This is how your views.py file should look like. Simply paste the code into your views.py, as well as the URL maps, and we&#8217;re ready to go:<\/p>\n<p><strong>Explanation of the above-mentioned code:<\/strong><\/p>\n<p>The above code demonstrates how the HttpResponse method is used to display any output to the screen. We define the cookie in a separate function and then use the request function with the variable\/expression COOKIE appended to it for displaying or receiving the cookie that has been set.<\/p>\n<p>The urls.py file will have the following structure:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83802\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__3.png\" alt=\"Android Cookie Implementation\" width=\"622\" height=\"116\" \/><\/a><\/p>\n<p><strong>Explanation of the above-mentioned code:<\/strong><\/p>\n<p>We define all the paths that are to be connected to each function that is in the views.py file with the python urls file.<\/p>\n<p>Guys, that&#8217;s it!! Now let&#8217;s test the server!!<\/p>\n<p>Use the command line to start the server:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">python manage.py runserver<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83803\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__4.png\" alt=\"Android Set Cookie\" width=\"1920\" height=\"927\" \/><\/a><\/p>\n<p style=\"text-align: center\"><strong>setcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83804\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Cookie-Implementation__5.png\" alt=\"Android get Cookie\" width=\"1920\" height=\"964\" \/><\/a><\/p>\n<p style=\"text-align: center\"><strong>getcookie<\/strong><\/p>\n<h4>Modify Cookie:<\/h4>\n<p>Let&#8217;s look at how we can modify a cookie now.<\/p>\n<h5>View.py<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def updating_cookie(request):\n    html = HttpResponse(\"We are updating  the cookie which is set before\")\n    html.set_cookie('TechVidvan','Updated Successfully')\n    return html<\/pre>\n<p>This new function will modify the previous function. Hence, we will have a modified cookie.<\/p>\n<h5>Urls.py<\/h5>\n<p>Make the following changes to the urls.py file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">path('update', views.updating_cookie),\n<\/pre>\n<h5>Run the server:<\/h5>\n<p>Visit all the pages in the following manner. You will come across these outputs.<\/p>\n<p><strong>1st output: \/setcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/modify-cookie__1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83808\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/modify-cookie__1.png\" alt=\"modify cookie in Android\" width=\"1920\" height=\"164\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>2nd output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83809\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__2.png\" alt=\"Django Modify Cookie\" width=\"1920\" height=\"242\" \/><\/a><\/p>\n<p><strong>3rd output: \/update<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83810\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__3.png\" alt=\"Updating Django Cookie\" width=\"1920\" height=\"164\" \/><\/a><\/p>\n<p><strong>4th output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__4-Copy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83811\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Modify-cookie__4-Copy.png\" alt=\"Django GetCookie\" width=\"1920\" height=\"214\" \/><\/a><\/p>\n<p>As you can see, the cookie&#8217;s value has been modified from the previously set value to the new value. This allows us to change or update the cookie value after it has been set.<\/p>\n<h4>Update Cookie:<\/h4>\n<p>Instead of using Httpresponse, we can use the redirect function to update a cookie. Yet, here we will use the set cookie function.<\/p>\n<p>Let\u2019s add a new function to it.<\/p>\n<h5>Views.py<\/h5>\n<p>Go on, make some changes again in the views.py file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def updating_cookie1(request):\n    html = redirect(setcookie)\n    html.set_cookie('TechVidvan','This is your Updated Page')\n    return html<\/pre>\n<h5>Run the server:<\/h5>\n<p>Execute the command and visit the below pages.<\/p>\n<p><strong>1st output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83812\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__1.png\" alt=\"Django Update Cookie\" width=\"1920\" height=\"214\" \/><\/a><\/p>\n<p><strong>2nd output: \/setcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83813\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__2.png\" alt=\"Django Getcookie\" width=\"1920\" height=\"164\" \/><\/a><\/p>\n<p><strong>3rd output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83814\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Update-cookie__3.png\" alt=\"Update Django Cookies\" width=\"1920\" height=\"189\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Delete Cookie in Django<\/h4>\n<p>Let&#8217;s take a look at how to delete a cookie that has already been placed.<\/p>\n<p>We already know that the set cookie function has an optional parameter called max_age that, by default, deletes the cookie session. We added the following code to the above code to make it a lot easier.<\/p>\n<h5>Views.py<\/h5>\n<p>Again, we need to work with the views.py file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def deleting_cookie(request):\n    html = HttpResponse(\"Deleting the cookie which is set\")\n    html.delete_cookie('TechVidvan','Updated Successfully')\n    return html<\/pre>\n<h5>Urls.py<\/h5>\n<p>And some changes in the urls.py file too.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">path('delete',views.deleting_cookie),<\/pre>\n<h5>Run the server:<\/h5>\n<p><strong>1st output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83805\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__1.png\" alt=\"Android Delete Cookie\" width=\"1920\" height=\"189\" \/><\/a><\/p>\n<p><strong>2nd output: \/delete<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83806\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__2.png\" alt=\"Delete Cookie in Android\" width=\"1920\" height=\"196\" \/><\/a><\/p>\n<p><strong>3rd output: \/getcookie<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83807\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/Delete-cookie__3.png\" alt=\"Android Cookie Handling\" width=\"1920\" height=\"535\" \/><\/a><\/p>\n<p><strong>A different approach is also available:<\/strong><\/p>\n<ul>\n<li>For handling the end of a cookie session, we also use an attribute called &#8220;expires.&#8221;<\/li>\n<li>You can experiment with the expire function and know how to delete a cookie.<\/li>\n<li>The following format is put in use to write the code:<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">response.cookies[\u2018cookie_name\u2019][\u2018expires\u2019] = datetime.today() + timedelta(days= number_of_days)\n<\/pre>\n<ul>\n<li>So that&#8217;s another way to delete the cookie.<\/li>\n<\/ul>\n<h3>Read Cookies from request<\/h3>\n<p>Every time a client requests something from a website, cookies are sent to that website. As a result, the server obtains a cookie with each request. Django makes retrieving data from a cookie very simple.<\/p>\n<h4>1. Using request.COOKIES[]<\/h4>\n<p>This is what we discussed above.<\/p>\n<h4>2. Using request.COOKIES.get()<\/h4>\n<p>Django additionally includes a way for retrieving the cookie&#8217;s desired value. Using the get method on the request object, you may directly get that value.<\/p>\n<p>The following is the syntax we&#8217;ll use:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">COOKIES.get(\u2018cookie_name\u2019, \u2018value\u2019)<\/pre>\n<p>Let&#8217;s make a few adjustments to our earlier example.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83815\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES.png\" alt=\"Django request.COOKIES\" width=\"646\" height=\"93\" \/><\/a><\/p>\n<p>We&#8217;ll need to redirect(), so import it now.<\/p>\n<p><strong>Code for setcookie:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def setcookie(request):\n    html = HttpResponse(\"&lt;h1&gt;Welcome to TechVidvan Employee Portal&lt;\/h1&gt;\")\n    if request.COOKIES.get('visits'):\n        html.set_cookie('TechVidvan', 'Welcome Back')\n        value = int(request.COOKIES.get('visits'))\n        html.set_cookie('visits', value + 1)\n    else:\n        value = 1\n        text = \"Welcome for the first time\"\n        html.set_cookie('visits', value)\n        html.set_cookie('TechVidvan', text)\n    return html\n<\/pre>\n<p>This code will determine whether the request contains cookies and then set the cookies accordingly.<\/p>\n<p>We&#8217;ll make use of the COOKIES.get().<\/p>\n<p>Make the necessary changes to our showcookie now ()<\/p>\n<p><strong>Code for showcookie:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def showcookie(request):\n    if request.COOKIES.get('visits') is not None:\n        value = request.COOKIES.get('visits')\n        text = request.COOKIES.get('TechVidvan')\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))\n        html.set_cookie('visits', int(value) + 1)\n        return html\n    else:\n        return redirect('\/setcookie')\n<\/pre>\n<p>This code will check for cookies, and then we&#8217;ll be able to run it.<\/p>\n<p>Simply launch your server and type getcookie\/ into the address bar.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83816\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES4.png\" alt=\"Django request.COOKIES\" width=\"1920\" height=\"1022\" \/><\/a><\/p>\n<p>You should get something like this as a result. The code then checks whether the cookie has a value and, if it doesn&#8217;t, redirects you to the setcookie function.<\/p>\n<p>In the URL bar, look for getcookie\/ once more.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-83817\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/07\/request.COOKIES5.png\" alt=\"Request Cookies in Django\" width=\"1920\" height=\"1012\" \/><\/a><\/p>\n<h3>Django Cookie Enable and Disable<\/h3>\n<p>The settings python file determines which cookies are enabled and disabled. The settings file contains session variables that can handle session cookies. Cookies are enabled and disabled manually by setting, updating, and deleting cookies. There are additional session-level cookies that can be set to true if necessary.<\/p>\n<p>By default, they are set to FALSE. These session cookies are secure since they are encrypted. We can use session cookies and update the number of counts whenever a specific website is visited by employing various techniques.<\/p>\n<h3>When you&#8217;re using cookies, keep this in mind.<\/h3>\n<ul>\n<li>Cookies should never be used to store sensitive information such as passwords. Because cookies store data in plain text, anyone can access and modify them.<\/li>\n<li>Most browsers won&#8217;t let you save more than 4KB of data in a cookie (i.e. 4KB for each cookie). Furthermore, most browsers will only accept up to 30 cookies per website.<\/li>\n<li>Remember that once the cookie is placed in the browser, it will be sent to the server with each request. Let&#8217;s imagine we&#8217;ve put 20 cookies, each 4KB in size, for a total of 80KB. That means that every time the browser sends a request to the server, it must send an additional 80KB of data!<\/li>\n<li>The cookies can be deleted at any time by the user. Users can even set their browsers to refuse to accept cookies altogether.<\/li>\n<\/ul>\n<h3>Issues with Django Cookie Security<\/h3>\n<ul>\n<li>Client Information can be misused.<\/li>\n<li>Users can be tracked.<\/li>\n<li>The client may delete the cookies.<\/li>\n<\/ul>\n<h3>Limitation of Django Cookies<\/h3>\n<ul>\n<li>Each cookie can hold up to 4096 bytes of data.<\/li>\n<li>Both the browser and the server can store cookies.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>Guys, that&#8217;s it!! This concludes our discussion on Django cookies and how to include them in your projects.<\/p>\n<p>We learned what a cookie is and how to deal with them in this article. We created, updated, and finally deleted the cookies. These cookies can help in the quick retrieval of data since when a user asks for information, the system does not always go to the database, search for it, and then return the results to the user. However, we must be cautious about hackers when dealing with these cookies.<\/p>\n<p>You&#8217;re now ready to make your cookies and use them the way you like.<\/p>\n<p>Do you have any questions or suggestions? Please feel free to share your thoughts below in the comments area.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django is a framework that allows us to interact with cookies. Cookies give you the possibility of storing and retrieving data that is saved in sessions. These cookies have an expiration date and are&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":83801,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3383],"tags":[4056,4057,4058],"class_list":["post-83711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-django-cookies","tag-django-cookies-handling","tag-limitation-of-cookies"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django Cookies Handling- Learn the Way to Set up Cookies in Django - TechVidvan<\/title>\n<meta name=\"description\" content=\"Cookies in Django are little text files that your browser creates and maintains in response to a specific Web-Server request. Learn more.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/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 Handling- Learn the Way to Set up Cookies in Django - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Cookies in Django are little text files that your browser creates and maintains in response to a specific Web-Server request. Learn more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-13T03:30:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django Cookies Handling- Learn the Way to Set up Cookies in Django - TechVidvan","description":"Cookies in Django are little text files that your browser creates and maintains in response to a specific Web-Server request. Learn more.","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:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/","og_locale":"en_US","og_type":"article","og_title":"Django Cookies Handling- Learn the Way to Set up Cookies in Django - TechVidvan","og_description":"Cookies in Django are little text files that your browser creates and maintains in response to a specific Web-Server request. Learn more.","og_url":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-08-13T03:30:44+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Django Cookies Handling- Learn the Way to Set up Cookies in Django","datePublished":"2021-08-13T03:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/"},"wordCount":1880,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg","keywords":["Django Cookies","Django Cookies handling","Limitation of cookies"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/","url":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/","name":"Django Cookies Handling- Learn the Way to Set up Cookies in Django - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg","datePublished":"2021-08-13T03:30:44+00:00","description":"Cookies in Django are little text files that your browser creates and maintains in response to a specific Web-Server request. Learn more.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Cookie-handling.jpg","width":1200,"height":628,"caption":"Django Cookies handling"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/django-cookies-handling\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Django Cookies Handling- Learn the Way to Set up Cookies in Django"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=83711"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/83801"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=83711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=83711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=83711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}