permissions in template

1,863 views
Skip to first unread message

masuran

unread,
Mar 12, 2007, 9:02:16 AM3/12/07
to Django users
Hello everyone,

I've created a custom permission on one of my models and I've given a
user the custom permission.

When I go into the shell (manage.py shell), I can confirm that the
user has the custom permission. But when I use the perms object in one
of my templates, it simple doesn't work.

{% if perms.custom_perm %}
do something
{% endif %}

According to the documentation, the perms object is included in the
template language. So I assume I don't have to create the object and
pass it to my template?
I've even tried the fix posted here http://code.djangoproject.com/ticket/2418
but to no avail. I still can't use the perms object in my template.

bgcooper

unread,
Mar 13, 2007, 2:15:26 AM3/13/07
to Django users
Funny that I should run into this post the same day that I'm trying to
work out how to do this.

I've got the same problem. From the shell:

>>> user = User.objects.get(id = 3)
>>> user.get_all_permissions()
Set(['ivms.can_play'])
>>> user.has_perm('ivms.can_play')
True

Fantastic. When I try and access it via my template it doesn't work.
e.g.

{% if perms.ivms.can_play %}
I have permissions
{% else %}
I don't have permissions
{% endif %}

Do I need to load any template tags to access this tag?

Note that I'm still using 0.95.

Thanks,
Ben


On Mar 13, 12:02 am, "masuran" <michael.ancka...@gmail.com> wrote:
> Hello everyone,
>
> I've created a custom permission on one of my models and I've given a
> user the custom permission.
>
> When I go into the shell (manage.py shell), I can confirm that the

> user has the custom permission. But when I use thepermsobject in one


> of my templates, it simple doesn't work.
>

> {% ifperms.custom_perm %}


> do something
> {% endif %}
>

> According to the documentation, thepermsobject is included in thetemplatelanguage. So I assume I don't have to create the object and
> pass it to mytemplate?
> I've even tried the fix posted herehttp://code.djangoproject.com/ticket/2418

masuran

unread,
Mar 13, 2007, 8:53:47 AM3/13/07
to Django users
I'm going to download the development version and try that one out,
might do the trick :)

bgcooper

unread,
Mar 14, 2007, 12:46:54 AM3/14/07
to Django users
Ahh, the authentication documentation (http://www.djangoproject.com/
documentation/authentication/) mentions that you need use
RequestContext to get user and perms in your template. We were simply
passing request.user into our template to get user information.

Here's how you do it. In your view

from django.template import RequestContext
return render_to_response("mytemplate.html",
context_instance=RequestContext(request))

Works for me!

Reply all
Reply to author
Forward
0 new messages