how to get a user's group in django code example
Example 1: django user group check
from django.contrib.auth.models import User, Group
group = Group(name="Author")
group.save()
user = User.objects.get(username="Johndoe")
user.groups.add(group)
if user.groups.filter(name=group):
else:
Example 2: user group template tag django
from django import template
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()