Get request path without having a request object

762 views
Skip to first unread message

cootetom

unread,
Jul 31, 2010, 11:56:54 AM7/31/10
to Django users
Hi all,

Is there any way of getting the request.path value without having the
request object that Django pass's around. Is there something similar
to os.environ for the web request where I can get the path?

I'm developing an app that needs to cache data on a page basis but the
data may come from any Python code that is run for the request or it
may come from the template layer via custom template tags. I don't
want to have to pass the request path around in order to eventually
pass it to my caching code for use in the cache key. I just want to be
able to find the request path from my caching code.

Possible?

cootetom

unread,
Jul 31, 2010, 12:27:39 PM7/31/10
to Django users
I think perhaps I'll also put this problem another way. I need to
cache data against the current web request without having the Django
built request object.

Carlos Daniel Ruvalcaba Valenzuela

unread,
Jul 31, 2010, 1:11:11 PM7/31/10
to django...@googlegroups.com
Just add django.core.context_processors.request to your
TEMPLATE_CONTEXT_PROCESSORS, this way you can access the current
request object in your template, you will have to use however
RequestContext class with render_to_response,

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

cootetom

unread,
Jul 31, 2010, 1:37:54 PM7/31/10
to Django users
Thanks Carlos but I'm trying to achieve getting the path without
having to pass the request object.

On Jul 31, 7:11 pm, Carlos Daniel Ruvalcaba Valenzuela

James Bennett

unread,
Jul 31, 2010, 11:23:07 PM7/31/10
to django...@googlegroups.com
On Sat, Jul 31, 2010 at 12:37 PM, cootetom <coot...@gmail.com> wrote:
> Thanks Carlos but I'm trying to achieve getting the path without
> having to pass the request object.

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."

cootetom

unread,
Aug 1, 2010, 8:18:27 AM8/1/10
to Django users
I have found some code that can get the request object.

f = sys._getframe()
while f:
request = f.f_locals.get('request')
if isinstance(request, HttpRequest):
path = request.path
break
f = f.f_back


James: I understand completely what you are saying and have thought
long and hard about how I can achieve what I'm doing without needing
the above code. I am creating an app that will provide a UI to admins
to translate the text on each web page. The app needs to keep a track
of which text from the current active language locale files has been
used in the rendering of the current page they're on. At the same time
I didn't want to have to change the way in which developers mark text
for translation. This meant that I needed to override the gettext and
template translation tags with custom ones which first remembered what
message ID's were being used and second just called the default Django
versions of these methods. However neither gettext or the template
translation tags need to be aware of the request but to remember which
message ID's are in use for a page I needed to know about the request.
My options were either change my overriding methods to also accept a
request object or find the object via another means. The latter seemed
easier on future development.

My app is almost finished. I need to do a load of testing on it. I'm
then going to place it into an existing site that uses locale files
for it's text. After that, I plan to release it for download so that
others can use it. It turns out really useful to have a UI that will
manage locale text. I have looked at another one called rosetta but it
was implemented into the admin system and so didn't have a way of
showing which text was used on specific pages which I think is really
useful, especially if clients want to get involved with text
translation.




On Aug 1, 4:23 am, James Bennett <ubernost...@gmail.com> wrote:

cootetom

unread,
Aug 3, 2010, 7:16:14 PM8/3/10
to Django users
I have finished and made this app available via it's own web site.
Take a look http://poedit.tomcoote.co.uk/

Sells, Fred

unread,
Aug 27, 2010, 9:22:20 AM8/27/10
to django...@googlegroups.com
I've looked at http://www.djangobook.com/en/beta/chapter12/ and the
section on Using sessions outside of views which shows:

>>> 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']

David De La Harpe Golden

unread,
Aug 27, 2010, 11:27:22 AM8/27/10
to django...@googlegroups.com
On 27/08/10 14:22, Sells, Fred wrote:

> 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

Reply all
Reply to author
Forward
0 new messages