Saturday, April 30, 2011

Re: user and perms context variables from auth context processor (in Django 1.2.3)

On Fri, Apr 29, 2011 at 9:22 AM, simon <kagwewest@gmail.com> wrote:

I am using Django 1.2.3. I was trying to check user permissions on a
certain model using the perms context variable from
contrib.auth.context_processors.auth. As per the documentation, I
tried this (foo is the app label, Bar is the model, and I'm using
default permissions):

{% if perms.foo.change_bar %} do stuff {% endif %}

It's always false even when logged in as a superuser. I tried doing
{{user.username}} to see if the user variable worked and it also
displayed nothing. So I checked in the debug toolbar and the variables
I get from the auth context processor are these:

{'messages':
<django.contrib.messages.storage.user_messages.LegacyFallbackStorage
object at 0xa38284c>,
 'perms': <django.utils.functional.__proxy__ object at 0xa2f236c>,
 'user': <django.utils.functional.SimpleLazyObject object at
0xa31748c>}

Is your view using a RequestContext to render the response? If not, that would explain why user does not appear to be set in your context. In order for context processors to affect your template's context, you must use a RequestContext for rendering. Context processors are not run for templates rendered with a regular Context.

Note the debug toolbar displaying the context processor and the variables it would set for the response don't imply that those variables were actually set in the context used to render the response: debug toolbar shows this information for all responses, regardless of whether a RequestContext was used to render the response.

Karen
--
http://tracey.org/kmt/

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