Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- from django.middleware.csrf import get_token
- _thread_locals = threading.local()
- def get_current_request():
- """Allows to get request object anywhere."""
- return getattr(_thread_locals, "request", None)
- def RequestHandlerMiddleware(get_response):
- def middleware(request):
- # Writes request object to local thread
- get_token(request)
- _thread_locals.request = request
- response = get_response(request)
- # Cleares request object from local thread
- _thread_locals.request = None
- return response
- return middleware
Add Comment
Please, Sign In to add comment