Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

django-session

django-session
django-cookie

Django Session
A session is a mechanism to store information on the server side during the interaction with the web application.

In Django, by default session stores in the database and also allows file-based and cache based sessions. It is implemented via a piece of middleware and can be enabled by using the following code.

Put django.contrib.sessions.middleware.SessionMiddleware in MIDDLEWARE and django.contrib.sessions in INSTALLED_APPS of settings.py file.

To set and get the session in views, we can use request.session and can set multiple times too.

The class backends.base.SessionBase is a base class of all session objects. It contains the following standard methods.

Image description

Django Session Example
The first function is used to set and the second is used to get session values.

//views.py

from django.shortcuts import render  
from django.http import HttpResponse  

def setsession(request):  
    request.session['sname'] = 'irfan'  
    request.session['semail'] = 'irfan.sssit@gmail.com'  
    return HttpResponse("session is set")  
def getsession(request):  
    studentname = request.session['sname']  
    studentemail = request.session['semail']  
    return HttpResponse(studentname+" "+studentemail);  
Enter fullscreen mode Exit fullscreen mode

Url mapping to call both the 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('ssession',views.setsession),  
    path('gsession',views.getsession)  
]  
Enter fullscreen mode Exit fullscreen mode

Run Server

$ python3 manage.py runserver 
Enter fullscreen mode Exit fullscreen mode

And set the session by using localhost:8000/ssession

Image description

Image description

Django Cookie

A cookie is a small piece of information which is stored in the client browser. It is used to store user's data in a file permanently (or for the specified time).

Cookie has its expiry date and time and removes automatically when gets expire. Django provides built-in methods to set and fetch cookie.

The set_cookie() method is used to set a cookie and get() method is used to get the cookie.

The request.COOKIES['key'] array can also be used to get cookie values.

Django Cookie Example
In views.py, two functions setcookie() and getcookie() are used to set and get cookie respectively

// views.py

from django.shortcuts import render  
from django.http import HttpResponse  

def setcookie(request):  
    response = HttpResponse("Cookie Set")  
    response.set_cookie('java-tutorial', 'javatpoint.com')  
    return response  
def getcookie(request):  
    tutorial  = request.COOKIES['java-tutorial']  
    return HttpResponse("java tutorials @: "+  tutorial);  
And 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)  
]  
Enter fullscreen mode Exit fullscreen mode

Start Server

$ python3 manage.py runserver 
Enter fullscreen mode Exit fullscreen mode

After starting the server, set cookie by using localhost:8000/scookie URL. It shows the following output to the browser.

Image description

And get a cookie by using localhost:8000/gcookie URL. It shows the set cookie to the browser.

Image description

Top comments (0)