[Answered ]-Persisting user selected data in Django application

2👍

This can easily be solved using user sessions. There’s already one built in, see Django’s sessions framework.

The first steps with Django’s default sessions using a database backend:

  1. Add django.contrib.sessions.middleware.SessionMiddleware to MIDDLEWARE_CLASSES in your settings.py

  2. Add django.contrib.sessions to INSTALLED_APPS in your settings.py

  3. Run manage.py migrate to create the database table that stores session data

Now you are able to use sessions in your views. From the docs:

When SessionMiddleware is activated, each HttpRequest object – the first argument to any Django view function – will have a session attribute, which is a dictionary-like object.

You can read it and write to request.session at any point in your view. You can edit it multiple times.

I would use a view to select an account and save it to the user’s session:

request.session['account'] = aid

Afterwards you can read this value like this:

request.session.get('account', None)

Leave a comment