how to get request object out of views

903 views
Skip to first unread message

Kejun He

unread,
Aug 8, 2011, 6:08:29 AM8/8/11
to django...@googlegroups.com
Hi,

I try to get a request object out of views.py, and could not get the reqeust object through "def demo(request)".

Could you tell me how to get it??

Or is there  other ways to get the same goal.

Thanks very much

regards,
kejun 

Daniel Roseman

unread,
Aug 8, 2011, 6:13:53 AM8/8/11
to django...@googlegroups.com
If you need the request somewhere outside of a views, you need to pass it in from there.

Since you don't say what your goal is, we can't advise on other methods to reach it.
--
DR.

Kejun He

unread,
Aug 8, 2011, 11:19:27 AM8/8/11
to django...@googlegroups.com
hi,

My goal is to generate some data,

For example:

I defined a template tag, and it is used to generate a menutree, the item of the menutree is a list.
The list  comes from another .py file.In this file,I want to get a current user object(LIKE:request.user),
 so I need to get a request object outside the views.

or other method to get the current user object is fine.

thanks

regards,
kejun


Tom Evans

unread,
Aug 8, 2011, 11:49:44 AM8/8/11
to django...@googlegroups.com

Put the current user into the template context, and pull the user out
of the context supplied to your template tag.

A common way of putting the current user into the template context is
to use a RequestContext to render the template with, and ensure that
'django.contrib.auth.context_processors.auth' is in
settings.TEMPLATE_CONTEXT_PROCESSORS (it is by default).

Template tags are passed the context by default when render() is
called on the Node returned by the template tag. If you are avoiding
most of the messiness by using the @simple_tag decorator, you can pass
@simple_tag(takes_context=True) to be passed the context. See [1].

Cheers

Tom

[1] https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#shortcut-for-simple-tags

Kejun He

unread,
Aug 8, 2011, 9:33:47 PM8/8/11
to django...@googlegroups.com
Hi,

May be you miss understanted my meaning.

I just want to get the current user object in a normal .py file out of views.

And this file just offer some data according to different user.

Do you have any methods to get the goal?

thank you

regards,
kejun 


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


Daniel Roseman

unread,
Aug 9, 2011, 4:23:23 AM8/9/11
to django...@googlegroups.com

On Tuesday, 9 August 2011 02:33:47 UTC+1, oops wrote:
Hi,

May be you miss understanted my meaning.

I just want to get the current user object in a normal .py file out of views.

And this file just offer some data according to different user.

Do you have any methods to get the goal?

thank you

regards,
kejun 

No, Tom understood you fine, and his answer was quite clear. Get the user from the template context into your tag, and pass it from there into the list function.
--
DR. 

Kejun He

unread,
Aug 9, 2011, 5:22:40 AM8/9/11
to django...@googlegroups.com
hi,
Ok, It is a good method to get the current user. I am sorry for that.

But i just do maintain a django project, and i do not want to change the template structure.

And now, I have found a new method to resolve the problem.

through a variable CURRENT_USER defined in settings.py to save the current user  in a view.
and get the current_user from settings.CURRENT_USER.

thanks for your reply.


And i found a strange appearance.

PART_ONE:
I defined a variable named CURRENT_USER

and import the settings in a view like below:

from gmadmin import settings      ################## the gmadmin is the name of the topo directory

then assign request.user to settings.CURRENT_USER

PART_TWO:
Get the settings.CURRENT_USER  in a .py file

the code:
import settings
user = settings.CURRENT_USER


But it reported a problem: the settings.CURRENT_USER is None .
and the problem disappeared when i use "import settings" instead of "from gmadmin import settings" on PART_ONE。


Could you talk about it?

regards,
kejun


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/LwpPt4GXW8sJ.

bruno desthuilliers

unread,
Aug 9, 2011, 5:54:54 AM8/9/11
to Django users
On Aug 9, 11:22 am, Kejun He <printer...@gmail.com> wrote:
> hi,
> Ok, It is a good method to get the current user.

It's actually THE good method.

>
> But i just do maintain a django project, and i do not want to change the
> template structure.
>
> And now, I have found a new method to resolve the problem.
>
> through a variable CURRENT_USER defined in settings.py to save the current
> user  in a view.
> and get the current_user from settings.CURRENT_USER.

How ? And does it really works ?-)
(hint: whatever you might think, it's totally broken)

> And i found a strange appearance.
>
> PART_ONE:
> I defined a variable named CURRENT_USER
>
> and import the settings in a view like below:
>
> from gmadmin import settings      ################## the gmadmin is the name
> of the topo directory

DONT import settings that way. ALWAYS use "from django.conf import
settings"

>
> then assign request.user to settings.CURRENT_USER

Question: what do you think will happen in a multithreaded
environment ?

> PART_TWO:
> Get the settings.CURRENT_USER  in a .py file
>
> the code:
> import settings
> user = settings.CURRENT_USER
>
> But it reported a problem: the settings.CURRENT_USER is None .
> and the problem disappeared when i use "import settings" instead of "from
> gmadmin import settings" on PART_ONE。
>
> Could you talk about it?

It would be better if you learned enough about Django and Python to
find out by yourself - and why this approach will never work.

In the meantime, save yourself some pain and do things the right way
(IOW: do has Tom said).

Tom Evans

unread,
Aug 9, 2011, 5:56:53 AM8/9/11
to django...@googlegroups.com

You should not do this.

https://docs.djangoproject.com/en/1.3/topics/settings/#altering-settings-at-runtime

If a function needs a user object, pass it a user object as an
argument. Using globals is a clear sign that you haven't understood
the problem. Don't do it. I gave you a precise way of achieving this
without futzing around with anti-patterns.

Cheers

Tom

Kejun He

unread,
Aug 9, 2011, 6:13:58 AM8/9/11
to django...@googlegroups.com
On Tue, Aug 9, 2011 at 5:54 PM, bruno desthuilliers <bruno.des...@gmail.com> wrote:
On Aug 9, 11:22 am, Kejun He <printer...@gmail.com> wrote:
> hi,
> Ok, It is a good method to get the current user.

It's actually THE good method.

>
> But i just do maintain a django project, and i do not want to change the
> template structure.
>
> And now, I have found a new method to resolve the problem.
>
> through a variable CURRENT_USER defined in settings.py to save the current
> user  in a view.
> and get the current_user from settings.CURRENT_USER.

How ? And does it really works ?-)
(hint: whatever you might think, it's totally broken)

> And i found a strange appearance.
>
> PART_ONE:
> I defined a variable named CURRENT_USER
>
> and import the settings in a view like below:
>
> from gmadmin import settings      ################## the gmadmin is the name
> of the topo directory

DONT import settings that way. ALWAYS use "from django.conf import
settings"

I test in my development server, and found that

from django.conf import settings   is to all client

but

import settings is to a singal one

>
> then assign request.user to settings.CURRENT_USER

Question: what do you think will happen in a multithreaded
environment ?

 I just test it on my development server, and it could work normally when there are several users.
 And have not test it in a  multithreaded environment.


> PART_TWO:
> Get the settings.CURRENT_USER  in a .py file
>
> the code:
> import settings
> user = settings.CURRENT_USER
>
> But it reported a problem: the settings.CURRENT_USER is None .
> and the problem disappeared when i use "import settings" instead of "from
> gmadmin import settings" on PART_ONE。
>
> Could you talk about it?

It would be better if you learned enough about Django and Python to
find out by yourself - and why this approach will never work.

In the meantime, save yourself some pain and do things the right way
(IOW: do has Tom said).

my method is not a normal way on django


thanks for your suggestion, I will spend enough time to learned django, i am so new

--
You received this message because you are subscribed to the Google Groups "Django users" group.

Kejun He

unread,
Aug 9, 2011, 6:14:58 AM8/9/11
to django...@googlegroups.com
hi,
I will do as your method

thanks for your reply

regards,
kejun

--
You received this message because you are subscribed to the Google Groups "Django users" group.

bruno desthuilliers

unread,
Aug 9, 2011, 6:47:27 AM8/9/11
to Django users
On Aug 9, 12:13 pm, Kejun He <printer...@gmail.com> wrote:
> On Tue, Aug 9, 2011 at 5:54 PM, bruno desthuilliers <
>
>
> > DONT import settings that way. ALWAYS use "from django.conf import
> > settings"
>
> I test in my development server, and found that
>
> from django.conf import settings   is to all client
>
> but
>
> import settings is to a singal one

I don't understand what you mean here. But anyway: the only correct
way to import your project's settings module is to use the first form.
It #1 respects the --settings option of ./manage.py (or the
$DJANGO_SETTINGS_MODULE environment variable - cf deploying on
mod_wsgi), and #2 makes sure you have the default values for what you
didn't set in you settings file.

>
> > > then assign request.user to settings.CURRENT_USER
>
> > Question: what do you think will happen in a multithreaded
> > environment ?
>
>  I just test it on my development server, and it could work normally when
> there are several users.

I'm talking about multithreaded environment here.

>  And have not test it in a  multithreaded environment.

Please do.

Reply all
Reply to author
Forward
0 new messages