Use Django Request.session Inside Utility Function
Solution 1:
You have to pass it. There is no other option.
request
is not some generic variable than you can just import from somewhere else. Rather, it is a set of information SPECIFIC to the particular request which was just made. It contains things like cookies and the current URL.
It is generated whenever someone makes a request via the server for it, which is why you see it as an argument for your view functions.
I suppose you could make it into some kind of global variable, but that's an absurd route to go.
Solution 2:
There is an option allow you to do that is by using this package: django-threadlocals. You should take a look at their code to see how it does.
Like so:
from threadlocals.threadlocals import get_current_session
defprocessData(data=None):
session = get_current_session()
# Do your stuff here
If you try to follow this approach, make sure that you understand what django-threadlocals
does. This was created because incase we want to use some data of the request object at deeper layers and you don't want to pass the request object through every single layer of closure.
This question also concern about should we use it or not, but the answer describe all the reason why we should use it, so please take a look.
Hope that helps!
Post a Comment for "Use Django Request.session Inside Utility Function"