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
To unsubscribe from this group, send email to django-users...@googlegroups.com.
--
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.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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 view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/LwpPt4GXW8sJ.
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
On Aug 9, 11:22 am, Kejun He <printer...@gmail.com> wrote:It's actually THE good method.
> hi,
> Ok, It is a good method to get the current user.
How ? And does it really works ?-)
>
> 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.
(hint: whatever you might think, it's totally broken)
DONT import settings that way. ALWAYS use "from django.conf import
> 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
settings"
Question: what do you think will happen in a multithreaded
>
> then assign request.user to settings.CURRENT_USER
environment ?
It would be better if you learned enough about Django and Python to
> 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?
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).
--
You received this message because you are subscribed to the Google Groups "Django users" group.
--
You received this message because you are subscribed to the Google Groups "Django users" group.