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