On Thu, Sep 2, 2010 at 3:30 PM, Daniel Klaffenbach
<danielklaffenb...@gmail.com> wrote:
> Hi,
>
> I am developing a Django app which relies on information from the
> request object a lot, especially request.user. I need this object (or
> let's say at least 'REMOTE_USER') in many models and I was wondering
> if there is another way to get the request object?
>
> In PHP there is something like $_SERVER which is a global variable
> containing for example the REMOTE_USER. I'm afraid I haven't found the
> Python/Django equivalent yet, but since it is a bit stupid to always
> pass the request object as a parameter to objects and functions I am
> sure there is another (better) approach.
>
> Could someone point out what I should do in this case?
>
>
> Regards,
> Dan Klaffenbach
>

Take the pain - part of django's design is that it isn't PHP, and
globals aren't exactly a good programming paradigm.

I would either pass the request object, or (better) actually pass the
objects/data that the function actually needs - eg request.user,
request.POST. This will be more verbose, but you (and future people
reading the code) will have a clearer understanding of what inputs the
function takes, and the function is not so readily tied to a
request/response cycle and so can be used outside of that context as
well.

Personally, I try to avoid passing the request to any function that is
not a view/middleware/etc, as it makes it harder to use that function
without a request.

Cheers

Tom

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

Reply via email to