Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django sessions

         ### Sessions expire when the user closes the browser ###
  
### add this line in settings.py ###
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

---------### Sessions expire after a period of inactivity ###------------------
  						### Fisrt way ###
  
### 1- set a timestamp in the session on every request ###
request.session['last_activity'] = datetime.now()
### 2- add a middleware to detect if the session is expired ###
### Note => search how to add a middleware ###
class SessionExpiredMiddleware:
    def process_request(request):
        last_activity = request.session['last_activity']
        now = datetime.now()

        if (now - last_activity).minutes > 10:
            # Do logout / expire session
            # and then...
            return HttpResponseRedirect("LOGIN_PAGE_URL")

        if not request.is_ajax():
            # don't set this for ajax requests or else your
            # expired session checks will keep the session from
            # expiring :)
            request.session['last_activity'] = now
            
---------### Sessions expire after a period of inactivity ###------------------
  						### second way ###
          
### add these lines in settings.py ###
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 10 # set just 10 seconds to test
SESSION_SAVE_EVERY_REQUEST = True # whenever you make new request, It saves the session and updates timeout to expire
### A session expired when you close the browser even if SESSION_COOKIE_AGE set ###
### Only when you are idle for more than 10 seconds, the session will expire ###
Comment

django set session variable

request.session['idempresa'] = idempresaValue
Comment

Session In Django

#you do not need to add anything

def index(request):

    request.session[0] = 'bar'
    return HttpResponse(request.session[0])
Comment

session in django


https://mefiz.com/  # For Developer
python-django
request.session["name"] = "name"  #views.py
value='{{ request.session.name }}' # example.html
https://www.youtube.com/momintechpro
Comment

PREVIOUS NEXT
Code Example
Python :: how to combine two lists in python 
Python :: is python a scripting language 
Python :: import matplotlib pyplot as plt 
Python :: q fields django Q objects 
Python :: how to pop an exact number from a list in python 
Python :: blur an image in python 
Python :: find the place of element in list python 
Python :: ValueError: cannot reshape array of size 98292 into shape (16382,1,28) site:stackoverflow.com 
Python :: empty array python 
Python :: python not in list 
Python :: test django migrations without applying them 
Python :: the shape of your array numpy 
Python :: beautifulsoup find text inside tag 
Python :: join 3 dataframes by index python 
Python :: example of break statement in python 
Python :: python multiply each item in list 
Python :: upload_file boto3 headers 
Python :: dicionario python 
Python :: Create list of unique values from dictionary 
Python :: list methods in python 
Python :: python check if string is url 
Python :: re.search() 
Python :: to divide or not to divide solution 
Python :: Django Abstract base classe 
Python :: how to append substring to string in specific position in python 
Python :: salvar plot python 
Python :: Solve linear equation with np.linalg.solve 
Python :: how to access a dictionary within a dictionary in python 
Python :: what is attribute in python 
Python :: rename colonne pandas 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =