From docs:
django.core.context_processors.request
If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every
RequestContext will contain a variable request, which is the current
HttpRequest. Note that this processor is not enabled by default;
you'll have to activate it.
Regards,
Carlos Daniel Ruvalcaba Valenzuela
In a word: don't.
Instead, design your system to pass the information you need where and
when you need it. This doesn't mean everything always has to sling
around a request object, just that you need to think carefully about
separation of concerns and which code needs to get at the request.
Do this, and in 6-12 months when you have to start making changes to
update your application, you'll be incredibly thankful that you did it
right the first time around.
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."
>>> from django.contrib.sessions.models import Session
>>> s =
Session.objects.get_object(pk='2b1189a188b44ad18c35e113ac6ceead')
But where does that pk come from?
I'm down deep in my module hierarchy and find I need a custom user
object that I stored in my session. I'm not using Django Authentication
because I'm running behind a legacy system that already does all that
and I had to be compatible.
I'm not sure of the thread safety of Django and wonder if I could store
this object as a local variable of some module like
From mysite.myapp import myPersistantStorage
myPersistantStorage.myUserObject = request.session['user']
> I'm not sure of the thread safety of Django and wonder if I could store
> this object as a local variable of some module like
No, that is not likely to work except in a single-threaded* context, and
even then it's a bit fraught (just being single-threaded still doesn't
mean each request is handled by a _new_ process, if you're not careful
you could leave things set to values from old requests).
There is a recipe out there for using a django middleware that uses
thread-local variables to stash info from the django request [1], but
that also tends to be frowned upon by django people for a variety of
reasons [2].
Python, while obviously somewhat lisp-oid, also doesn't have lisp-like
dynamics that might be idiomatic in lisp land.
Sooo.... just give up and explicitly pass the datum you want down
through the call chain. That can be a pain, but at least python has the
*args/**kwargs mechanism that can make it fairly easy for one callable
to just pass on through unused kwargs to another (a pattern used quite a
bit in django itself).
(* aside: single-threaded multiple-process serving is often a viable
option for serving stuff, at least on linux, where processes are very
cheap, and does avoid threading issues nicely (remember ordinary python
2.x can be kinda sucky threading-wise anyway [3]). Ex-windows people on
linux/unix often jump to threading very prematurely, mistakenly
generalising from windows [4])
[1] http://djangosnippets.org/snippets/1605/
[2] http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
[3] http://en.wikipedia.org/wiki/Global_Interpreter_Lock
[4]
http://stackoverflow.com/questions/47845/why-is-creating-a-new-process-more-expensive-on-windows-than-linux