As far as I understand, flask.g
offers temporary storage for the current request context (even though it's technically the application context as described here). Accessing g.my_data
during a request handler will ensure that my_data
is for the current request. Does Django have something equivalent to this?
In my experimentation, Django's request
object, which is passed into view functions, can be used the same as flask.g
. I can simply use request.my_data
and be ensured that my_data
is for the current request.
Noticing, this I tried using flask.request
similar to how I used flask.g
, with equivalent results. This begs the question what does flask.g
provide over flask.request
, just peace of mind that flask.request
attributes will not be overwritten?
FYI on use case, I'm sharing data between the actual request handler (flask) or view functions (django), and the middleware (django) or @before_request (flask) handlers.
This source seems to recommend putting data on the request.
As does this source.
This leads me to answer yes for number 1 below, but wonder even more about number 2??
TLDR:
- Can Django
request
be used equivalently toflask.g
? - Can
flask.request
be used equivalently toflask.g
, or what is the benefit offlask.g
overflask.request
?