How to experiment when session is and isn't saved in Django?
Asked Answered
L

2

0

I'm trying to experiment when session is and isn't saved with the 4 cases of code below in When sessions are saved but I don't know how to do it:

# Session is modified.
request.session["foo"] = "bar" # The 1st case

# Session is modified.
del request.session["foo"] # The 2nd case

# Session is modified.
request.session["foo"] = {} # The 3rd case

# Gotcha: Session is NOT modified, because this alters
# request.session['foo'] instead of request.session.
request.session["foo"]["bar"] = "baz" # The 4th case

I'm not going to use the code below:

request.session.modified = True

And, I set SESSION_SAVE_EVERY_REQUEST False in settings.py as shown below:

# "settings.py"

SESSION_SAVE_EVERY_REQUEST = False

So, how can I experiment when session is and isn't saved with the 4 cases of code above?

Linc answered 5/7, 2023 at 15:51 Comment(0)
C
1

The session can only check flat elements. If you want to store an object or dictionary within a dictionary, it will not trigger the session changed status, and you will need to do it yourself.

request.session["foo"]["bar"] = "baz"
# Gotcha: Session is NOT modified, because this alters
# request.session['foo'] instead of request.session.

more info here: https://docs.djangoproject.com/en/4.2/topics/http/sessions/#when-sessions-are-saved

Chanel answered 5/7, 2023 at 19:41 Comment(0)
L
0

You should experiment each case one by one with these steps below. *I use Django 4.2.3.

The 1st run with the code below saves the session (The 1st case):

# "views.py"

from django.http import HttpResponse

def test(request):
    request.session["foo"] = "bar" # Here
    return HttpResponse('Test')

The 2nd run with the code below shows that the session is successfully saved (The 1st case):

# "views.py"

from django.http import HttpResponse

def test(request):
    print(request.session.get('foo')) # bar
    return HttpResponse('Test')

The 3rd run with the code below saves the session (The 2nd case):

# "views.py"

from django.http import HttpResponse

def test(request):
    del request.session["foo"] # Here
    return HttpResponse('Test')

The 4th run with the code below shows that the session is successfully saved (The 2nd case):

# "views.py"

from django.http import HttpResponse

def test(request):
    print(request.session.get('foo')) # None
    return HttpResponse('Test')

The 5th run with the code below saves the session (The 3rd case):

# "views.py"

from django.http import HttpResponse

def test(request):
    request.session["foo"] = {} # Here
    return HttpResponse('Test')

The 6th run with the code below shows that the session is successfully saved (The 3rd case):

# "views.py"

from django.http import HttpResponse

def test(request):
    print(request.session.get('foo')) # {}
    return HttpResponse('Test')

The 7th run with the code below doesn't save the session (The 4th case):

# "views.py"

from django.http import HttpResponse

def test(request):
    request.session["foo"]["bar"] = "baz" # Here
    return HttpResponse('Test')

The 8th run with the code below shows that the session is not successfully saved (The 4th case)

# "views.py"

from django.http import HttpResponse

def test(request):
    print(request.session.get('foo')) # {}
    return HttpResponse('Test')
Linc answered 6/7, 2023 at 15:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.