Created
October 8, 2013 10:04
-
-
Save Brick85/6882478 to your computer and use it in GitHub Desktop.
Global requests for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
def get_request(): | |
"""Walk up the stack, return the nearest first argument named "request".""" | |
frame = None | |
try: | |
for f in inspect.stack()[1:]: | |
frame = f[0] | |
code = frame.f_code | |
if code.co_varnames[:1] == ("request",): | |
return frame.f_locals["request"] | |
elif code.co_varnames[:2] == ("self", "request",): | |
return frame.f_locals["request"] | |
finally: | |
del frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment