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

Session In Django

#you do not need to add anything

def index(request):

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

django sessions for beginners

def index(request):
    ...

    num_authors = Author.objects.count()  # The 'all()' is implied by default.
    
    # Number of visits to this view, as counted in the session variable.
    num_visits = request.session.get('num_visits', 0)
    request.session['num_visits'] = num_visits + 1

    context = {
        'num_books': num_books,
        'num_instances': num_instances,
        'num_instances_available': num_instances_available,
        'num_authors': num_authors,
        'num_visits': num_visits,
    }
    
    # Render the HTML template index.html with the data in the context variable.
    return render(request, 'index.html', context=context)
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 :: number of column in dataset pandas 
Python :: how to get the ip address of laptop with python 
Python :: python list remove at index 
Python :: strftime 
Python :: importing database in dataframe using sqlalchemy 
Python :: how to import numpy in python 
Python :: pandas dataframe sort by column name first 10 values 
Python :: filter one dataframe by another 
Python :: Kill python background process 
Python :: python insert to sorted list 
Python :: turn a list into a string python 
Python :: numpy array with 2 times each value 
Python :: python function get number of arguments 
Python :: create virtual environments python 
Python :: make a list in python 3 
Python :: import antigravity in python 
Python :: python num2words installation 
Python :: python move cursor to previous line 
Python :: How are iloc and loc different? 
Python :: contextlib.subppress python 
Python :: how to code python 
Python :: python create venv 
Python :: django execute 
Python :: Roman to integer with python 
Python :: pytube 
Python :: django prefetch_related vs select_related 
Python :: matplotlib bar chart 
Python :: check if element in list python 
Python :: split a string into N equal parts. 
Python :: creating numpy array using zeros 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =