Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Global Request variable in Python/Django available?

Tags:

python

django

I have written a plugin that sends a signal to activate my code. However, it doesn't send the user-request object to my code. I am looking for a way to retrieve the current request without modifying the main application. I cannot find any documentation related to global request (like $_SERVER['REMOTE_ADDR'] in PHP).

I would like to know if there are any variable to do like that in Python/Django.

like image 298
scalopus Avatar asked Sep 01 '11 08:09

scalopus


3 Answers

Django doesn't provide a global request object (it would actually be a thread local, not a global). But there are a few techniques you can use to get the same effect yourself: http://nedbatchelder.com/blog/201008/global_django_requests.html

like image 181
Ned Batchelder Avatar answered Oct 08 '22 14:10

Ned Batchelder


AFAIK it is not available, except you make it available.

You can copy+paste the snippets provided in the other answers, or you can use this library: https://pypi.python.org/pypi/django-crequest

Middleware to make current request always available.

like image 20
guettli Avatar answered Oct 08 '22 14:10

guettli


you can attach it to current request via middleware and retrieve it back https://github.com/jedie/django-tools/blob/master/django_tools/middlewares/ThreadLocal.py

like image 33
woryzower Avatar answered Oct 08 '22 13:10

woryzower