3

I'm having a small issue with my permissions in my Django template.

I'm trying to, based on permissions, show an icon in the menu bar for my project. I want to have it so that if the user has the permissions to add a new follow-up to the project, they can see the icon, if they don't have that permission, then do not display the link.

My permission syntax is follow.add_followup, which I got from printing user.get_all_permissions().

I have tried this code in my template:

...
{% if user.has_perm('followup.add_followup') %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
...

But when I display the template, I am presented with this error:

TemplateSyntaxError at /project/232/view/

Could not parse the remainder: '(followup.add_followup)' from 'user.has_perm(followup.add_followup)'

Any thoughts? This has been giving me a headache! :)

Community
  • 1
  • 1
TheLifeOfSteve
  • 3,208
  • 6
  • 37
  • 53

4 Answers4

11

Since you are using the Django permission system, it's better you use the followingg template syntax...

{%if perms.followup.add_followup%}your URL here{%endif%}

EDIT: Django automatically creates 3 permissions for each model, 'add', 'change' and 'delete'. If there exists no model for adding a link, then you must add the permission from related model, in the model class Meta... Likewise:

somemodels.py

class SomeModel(Model):
    ...
    class Meta:
    permissions = (('add_followup','Can see add urls'),(...))

In the Django auth user admin page, you can see your permission. In the template layer, permission is presented with the basic Django style,

<app_label>.<codename>

which, in this case, will be like:

{%if perms.somemodels.add_followup%}your URL here{%endif%}

If there is no model, related to the job you wish to do, the add the permission to a model...

In your template, you can write

{{perms.somemodels}}

to seal available permissions to that user, where somemodel is the name of the applicaton that you add your permission to one of its models.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mp0int
  • 18,172
  • 15
  • 83
  • 114
2

This is my very simple solution, in your template add this:

for example:

.......

{% if 'user.can_drink' in user.get_all_permissions %}
   {{ user }} can drink.
   .......
{% else %}
   {{ user }} can´t drink.
    ........
{% endif %}

.......
Liberathor
  • 29
  • 6
2

Django documentation detailing answer #2: https://docs.djangoproject.com/en/dev/topics/auth/#id9

The currently logged-in user's permissions are stored in the template variable {{ perms }}. This is an instance of django.contrib.auth.context_processors.PermWrapper, which is a template-friendly proxy of permissions.

Kevin Audleman
  • 334
  • 2
  • 4
  • Thank you... this is by far the most useful answer! – Greg Feb 21 '13 at 02:25
  • Updated link: https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions - works for Django 1.5 to 1.7 (which is latest at time of writing). – Rob Grant Aug 21 '14 at 08:13
1

I have tried this code in my template:

This kind of complex decision-making goes in the view functions.

Or it goes into the context which is then presented to the template.

https://stackoverflow.com/search?q=%5Bdjango%5D+context

When to use context processor

Do this in your view

def my_view( request ):
    followup= user.has_perm('followup.add_followup')
    # etc.
    return render_to_response( template, {'followup':followup,... )

Then your template is simply

{% if followup %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
Community
  • 1
  • 1
S.Lott
  • 384,516
  • 81
  • 508
  • 779