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 shell
from django.contrib.auth.models import User
david = User.objects.get(username="david")

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 '' %}">Management</a></li>
{% endif %}


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


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>
  • Lines and paragraphs break automatically.

More information about formatting options

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