[Django]-Django Session Variable resetting

7👍

request.session['foo'].append('bar') does not affect session. Only request.session['...'] = .../del request.session['...'] affect the session.

Try following code.

request.session['foo'] = ['bar']

https://docs.djangoproject.com/en/dev/topics/http/sessions/#when-sessions-are-saved

By default, Django only saves to the session database when the session
has been modified – that is if any of its dictionary values have been
assigned or deleted:

# Session is modified.
request.session['foo'] = 'bar'

# Session is modified.
del request.session['foo']

# Session is modified.
request.session['foo'] = {}

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

In the last case of the above example, we can tell the session object
explicitly that it has been modified by setting the modified attribute
on the session object:

request.session.modified = True

Leave a comment