• JavaScript
  • JSP
  • RubyonRails
  • Core Java
  • SQL Tutorial
  • jQuery
  • Servlet
  • Java IO

  • <

    Django Session Tutorial

    The session framework store and retrieve random data on a per-site-visitor basis.It stores data on the server side.Cookies contain a session ID .It is accessed by using middleware at setting.py file.

    Request.Session is used to get the session in views.Session cookies will be stored in users browsers for as long as SESSION_COOKIE_AGE.

    Session keys begin with an underscore are reserved for internal use by Django.Session are saved when the session has been modified.You can retrive and set session by using request.getsession and request.setsession.

    Django Session Methods

  • __getitem__(key) Get session value.
  • __setitem__(key, value)Set session value.
  • __delitem__(key) Delete session object.
  • __contains__(key) It checks whether the container contains the particular session object or not.
  • get(key, default=None) Get session value of the specified key.

  • Django Session Example

    from django.shortcuts import render  
    from django.http import HttpResponse  
    def setsession(request):  
        request.session['name'] = 'prady'  
        return HttpResponse("session is set")  
    def getsession(request):  
        fname = request.session['name']  
        return HttpResponse(fname);  

    from django.contrib import admin  
    from django.urls import path  
    from myapp import views  
    urlpatterns = [  
        path('admin/', admin.site.urls),  
        path('index/', views.index),  

    © copyright 2017-2021 Completedone pvt ltd.