permission of Django

Asked

Viewed 246 times

0

Good afternoon, I am wanting in the template to make a loop to disable a button according to the custom permission .. The model code is:

# -*- coding: utf8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User

class PermUser(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    tecnico = models.ForeignKey('imuno.Tecnico')

    def __str__(self):
        return ''

    class Meta:
        permissions = (
                       ("SIR", "Somente Imprimir Relatorio"),
                      )

In the template I do:

{% if show_save %}
       {% if perm.permissao.SIR %}
            <button type="submit" title="{% trans 'Save' %}" class="list-group-item disabled" name="_save">
              <span class="glyphicon glyphicon-floppy-disk"></span>
              <span class="text">{% trans 'Save' %}</span>
            </button>
       {% else %}
            <button type="submit" title="{% trans 'Save' %}" class="list-group-item active" name="_save">
              <span class="glyphicon glyphicon-floppy-disk"></span>
              <span class="text">{% trans 'Save' %}</span>
            </button>
       {% endif %}
 {% endif %}

With or without permission it shows the button if I put "not" with or without permission is disabled. Would anyone know what the error is? in the Django documentation shows this way to apply the perm call in the template.

  • What is inside the variable perm? You tried debugging by printing {{ perm.permissao.SIR }} and seeing if what you’ve been waiting for is coming through this variable?

  • The variable that stores permissions is "perms" (https://docs.djangoproject.com/pt-br/1.10/topics/auth/default/#Permissions)

  • So it really is perms, it is always returning True even if it is with permission or not. as if removing it did not update.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.