Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Django already has a groups and permissions system, which may be sufficient for your purpose.</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/auth/" rel="noreferrer">http://docs.djangoproject.com/en/dev/topics/auth/</a></p> <p>Generally in your code you check if a user has a permission. A user has his own permissions and those of the groups he belongs to. You can administer this pretty easily from the admin console.</p> <p>There are two parts you need to look at.</p> <ol> <li>Check that a user requesting a page has permission to do so.</li> <li>Only display links to the user if he has the permission.</li> </ol> <p>For 1. you can check permissions in a decorator as such:</p> <pre><code>from django.contrib.auth.decorators import permission_required @permission_required('polls.can_vote') def some_view(request): </code></pre> <p>For 2. the currently logged-in user's permissions are stored in the template variable {{ perms }}. This code checks the same permission as above.</p> <pre><code>{% if perms.polls.can_vote %} &lt;a href="/vote"&gt;vote&lt;/a&gt; {% endif %} </code></pre> <p>To generate a list of links you can iterate over user.get_all_permissions() and fetch the links (or function that generates the link) from a dict:</p> <pre><code>def more_elaborate_list_of_links_for_a_perm(user): return ["/link1", ...] _LINKS = { 'polls.can_vote' : lambda u: ["/user/specific/link/" + u.id], 'polls.can_close': lambda u: ['/static/link/1', 'static/link/2'], 'polls.can_open' : more_elaborate_list_of_links_for_a_perm } def gen_links(user): # get_all_permissions also gets permissions for users groups perms = user.get_all_permissions() return sum((_LINKS[p](user) for p in perms if p in _LINKS), []) </code></pre> <p>There are probably many other approaches.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload