from django.contrib.auth.decorators import permission_required
# permission = 'applicationName.permission'
# Ex.
permission = "main.add_post"
@permission_required(permission, login_url="/login", raise_exception=True)
def view(request):
...
class USCitizen(models.Model):
# ...
class Meta:
permissions = (
("can_drive", "Can drive"),
("can_vote", "Can vote in elections"),
("can_drink", "Can drink alcohol"),
)
group.permissions.set([permission_list])
group.permissions.add(permission, permission, ...)
group.permissions.remove(permission, permission, ...)
group.permissions.clear()
from myapp.models import BlogPost
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(BlogPost)
permission = Permission.objects.create(
codename='can_publish',
name='Can Publish Posts',
content_type=content_type,
)
{% if perms.app_label.can_do_something %}
<form here>
{% endif %}