Create permissions on Django

Asked

Viewed 249 times

1

I was trying to create a permission for my template but I couldn’t. I did some research to know how to do it and I’m not getting it. basically would like a permission similar to what Django has for example the is_superuser.

in the template I can define that only super users can view the content.

{% if user.is_superuser %}
    <p> Este código só pode ser visto por um super usuário.</p>
{% endif %}

this is what I use for super users but I want to make a specific type of user example:

class UserTecnico(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
cpf = models.CharField(primary_key=True,max_length=11,blank=True)
SEXO_CHOICES = (
('M', 'Maculino'),
('F', 'Feminino'),
)
sexo = models.CharField(max_length=2,blank=True,choices=SEXO_CHOICES)
is_pode_acessar = models.BooleanField(default=True)

for example here I have a technical user with a boolean field "is_pode_access" the default is True but nothing happens in the template.

    {% if UserTecnico.is_pode_acessar %}
       <p> Este código só pode ser visto por um usuário técnico.</p>
    {% endif %}

Can someone tell me how I do ?

1 answer

0

Once you’ve defined the relationship OneToOne between the User and the UserTecnico you can do the validation with:

{% if user.usertecnico.is_pode_acessar %}
    <p> Este código só pode ser visto por um usuário técnico.</p>
{% endif %}
  • Thank you! I’ll try.

  • worked out the way you suggested thanks!

  • Good! Mark the answer as right to be able to help others with the same question. Thank you

  • but I don’t have enough points to score :/

Browser other questions tagged

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