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:
-
Add
django.contrib.sessions.middleware.SessionMiddleware
toMIDDLEWARE_CLASSES
in yoursettings.py
-
Add
django.contrib.sessions
toINSTALLED_APPS
in yoursettings.py
-
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)