I use the perms object in my templates.
{% if perms.app_name.fooperm %}
{% endif %}
See https://docs.djangoproject.com/en/2.0/topics/auth/default/#permissions


From: [email protected] [mailto:[email protected]] On 
Behalf Of guettli
Sent: Friday, December 15, 2017 8:09 AM
To: Django users
Subject: custom permission strings: Avoid magic strings ...

Hi,

We define the permission like in the docs: 
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#custom-permissions

To avoid the magic string 'fooapp.fooperm' in the code which uses the 
permission very often, I choose this solution:

{{{
# file models.py
class FooModel(Model):

    FOOPERM_PERMISSION_STRING='fooapp.fooperm'

    class Meta:
        permissions = (
            ('fooperm', 'Foo Permission'),
        )
}}}


{{{
# file views.py
# This and other files use the permission string very often.

...
    if request.user.has_perm(FooModel.FOOPERM_PERMISSION_STRING):
        ...
}}}


But still I am not happy with the above solution. I think it looks ugly.

How do you avoid the magic permission string in your code?

Regards,
  Thomas Güttler

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/21f0b282-66fb-46fb-9513-8f4db19b4cf1%40googlegroups.com<https://groups.google.com/d/msgid/django-users/21f0b282-66fb-46fb-9513-8f4db19b4cf1%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/af4a77b44fa843e1b16a1594ec281dd9%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to