Skip to main content

Session Tutorials in Django



Session Tutorials in Django:

A session is a special object in Django that can store the data of an application that can be created by one method and used in multiple methods.

A session is also called a server-side object that provides persistent data to use on 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 store data under client machine by Browser Cookie.


Syntax to Create Session

request.session['key'] = value


Syntax to get data from the session

var=request.session['key']


How to destroy session object:-   

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

del request.session['key']


Syntax to validate session:

if(request.session.has_key('key')):
        write code that execute after session set


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')    


Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...