Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Session Tutorials in Django

A session is a special variable in Django which can store the data of an application that can be created one views.py single method and used into multiple methods.

A session is also called server-side objects that provide persistent data to use into multiple screens.


When we create a login option then the session provides individual user data, login time, logout time, page security, etc.


It is called Server Side Object because it will be created by Web Server and in client machine session data will be managed by Browser Cookie.


Syntax to Create Session


request.session['username'] = value

Syntax to get data from the session

var=request.session['username']

How to destroy session object:-   

when we create login then the logout option is mandatory. we will manually logout session data using.

del request.session['username']


Complete Code of Views.py


from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import Contact,Register,Feedback


def index(request):
return render(request,"scsapp/index.html")

def about(request):
return render(request,"scsapp/about.html")

def service(request):
return render(request,"scsapp/services.html")

def login(request):
    return render(request,"scsapp/login.html")

def logincode(request):
    e=request.POST["txtemail"]
    p=request.POST["txtpass"]
    s = Register.objects.filter(emailid=e,password=p)
   
    if(s.count()==1):
        request.session['uid']=e
        return redirect('dashboard')
    else:
        return render(request,"scsapp/login.html",{'msg':'invalid userid and password'})

def contact(request):
return render(request,"scsapp/contact.html")

def viewcontact(request):
    s=Contact.objects.all()
    return render(request,"scsapp/viewcontact.html",{'res':s})

def Editcontact(request):
    s = Contact.objects.get(pk=request.GET["q"])
    return render(request,"scsapp/editcontact.html",{'res':s}) 
def edit(request):
    e=request.POST["txtemail"]
    m=request.POST["txtmobile"]
    msg=request.POST["txtmsg"]
    s = Contact.objects.get(pk=request.POST["txtid"])
    s.emailid=e
    s.mobile=m
    s.message=msg
    s.save()
    return redirect('viewcontact')

def Deletecontact(request):
    s = Contact.objects.get(pk=request.GET["q"])
    s.delete()
    return redirect('viewcontact')
def contactcode(request):
    e=request.POST["txtemail"]
    m=request.POST["txtmobile"]
    msg=request.POST["txtmsg"]
    obj = Contact(emailid=e,mobile=m,message=msg)
    obj.save()
    return redirect('viewcontact')
   # return render(request,"scsapp/contact.html",{'res':'data submitted successfully'})

def editcontact(request):
    return render(request,"scsapp/editcontact")
def deletecontact(request):
    return render(request,"scsapp/deletecontact")
def gallery(request):
return render(request,"scsapp/gallery.html")

def dashboard(request):
    if(request.session.has_key('uid')):
     data = request.session['uid'] 
     return render(request,"scsapp/dashboard.html",{'u':data})
    else:
     return render(request,"scsapp/login.html")

def dashcode(request):
    e=request.POST["txtemail"]
    m=request.POST["txtto"]
    msg=request.POST["txtdesc"]
    obj = Feedback(emailid=e,feeddesc=msg,feedto=m)
    obj.save()
    return render(request,"scsapp/dashboard.html",{'msg':'feedback submitted successfully'})

def logout(request):
    del request.session['uid']

    return redirect('login')    


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post