I want to prepare my Django webservice for distributed requests using cloud computing (stateless!). Therefore I need to make my requests completely modular and take care that no data from a previous request are used. Since I need a lot of data and functions for some requests, it is not reasonable to pass these data to each and every function.
Question: Which possibilities are there to store data "globally" for a single request?
As far as I see there are two possibilities, but both of them do not seem to meet my needs:
1. Django Session
Store data in request.session (using the middleware 'django.contrib.sessions.middleware.SessionMiddleware').
When I understood it properly, the data are still kept after the request for the next requests of the same user until the user logs out.
Is this correct?
In my tests the data are empty for each new request. But I don't understand why. And still I don't have a handle to the request in all of my functions.
For more info about Django sessions see:
- https://docs.djangoproject.com/en/3.0/topics/http/sessions/
- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Sessions
- clear session data after multi-page form submit in Django
- Django/Auth: logout clears the session data?
2. Python Global Keyword
Store data in python global variables.
These data are stored independently of Django requests and sessions.
For more info about global variables see:
Workaraound
As an ugly workaround for both solutions I can delete the "global" data at the end of each request to keep each request independent from others.
Is there a better way to go?
How is this situation meant in Django?
Thank you in advance for your kind support.