User login

Figuring out what Django permission names are and what permissions a user has

You can see the permissions a particular user has, in the 'machine-readable' form Django uses, quite easily through the Django shell. For a user with the username 'david', for example:

$ python manage.py shell
from django.contrib.auth.models import User
david = User.objects.get(username="david")
david.get_all_permissions()
{u'evaluations.add_evaluation',
 u'evaluations.change_evaluation',
 u'reports.view',
 u'reports.view_all'}

Both this and the template form (see below) correctly see permissions that are directly added to a user and those which are associated through groups.

So it seems the undocumented convention/rule is that the permission listed on the Django admin page, in the form "evaluations | evaluation | Can add evaluation" or "evaluations | evaluation | Can change evaluation" translates to dots replacing the pipes, dropping the model section entirely and also the word "Can" being dropped, and underscores replacing spaces, in the form "evaluations.add_evaluation" or "evaluations.change_evaluation".

Using a permission as a conditional in a template looks like this:

{% if perms.evaluations.change_evaluation %}
  <li><a href="{% url 'overview.open' %}">Management</a></li>
{% endif %}

Reference

https://docs.djangoproject.com/en/1.8/ref/contrib/auth/

https://www.chicagodjango.com/blog/testing-different-django-user/

http://stackoverflow.com/questions/17620783/django-access-admin-user-in-python-shell

http://stackoverflow.com/questions/2081061/django-user-get-all-permissions-is-empty-while-user-permissions-is-set

http://stackoverflow.com/questions/16969009/checking-for-permission-in-django-template-not-working

http://stackoverflow.com/questions/9469590/check-permission-inside-a-template-in-django

http://stackoverflow.com/questions/4778685/how-do-i-use-django-groups-and-permissions

Searched words: 
django template user has perm django how to see machine name for permission django shell log in user django shell load user permissions in template django find permission name django template user has perm

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.