Sunday, August 1, 2010

Re: Get request path without having a request object

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:
> On Sat, Jul 31, 2010 at 12:37 PM, cootetom <coote...@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."

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment