1.

How to clear cache in Django

Answer»

A piece of information stored in the CLIENT browser is called cookies and itis used to store user's data in a file. Cookies can be stored permanently or for the specified time.

Cookies are removed automatically when expired. Django has built-in methods to set and fetch cookies.

The set_cookies() method can be used to set cookies. The getcookies() method can be used to get the cookies. The request.COOKIES['key'] ARRAY is used to get cookies value.

Let us see the example of Django COOKIE -

Two functions setcookies() and getcookie() are used to set and get the cookie in views.py.

// views.py     from django.shortcuts import render       from django.http import HttpResponse       def setcookie(request):           response = HttpResponse("Cookie Set")           response.set_cookie('selenium-tutorial', 'nikasio.com')           return response       def getcookie(request):           tutorial  = request.COOKIES['selenium-tutorial']           return HttpResponse("selenium tutorials @: "+  tutorial);

Update URLs specified to access these functions -

//urls.py     from django.contrib import admin       from django.urls import PATH       from myapp import views       urlpatterns = [           path('admin/', admin.site.urls),           path('index/', views.index),           path('scookie',views.setcookie),           path('gcookie',views.getcookie)       ]  

Execute below command to start server -

  • $ PYTHON3 manage.py runserver 
  • Once the server is up, set cookies by using localhost:80000/scookie url and get cookies by using localhost:8000/gcookie URL.


Discussion

No Comment Found