[Django]-User permissions for Django module

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 %}
👤S.Lott

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.

👤Mp0int

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.

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

.......

Leave a comment