Skip to content

Instantly share code, notes, and snippets.

@Brick85
Created October 8, 2013 10:04
Show Gist options
  • Save Brick85/6882478 to your computer and use it in GitHub Desktop.
Save Brick85/6882478 to your computer and use it in GitHub Desktop.
Global requests for Django
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