I have similar situation, I wanted to test if the user is in a certain group. So, I've created new file utils.py where I put all my small utilities that help me through entire application. There, I've have this definition:
utils.py
def is_company_admin(user):
return user.groups.filter(name='company_admin').exists()
so basically I am testing if the user is in the group company_admin and for clarity I've called this function is_company_admin.
When I want to check if the user is in the company_admin I just do this:
views.py
from .utils import *
if is_company_admin(request.user):
data = Company.objects.all().filter(id=request.user.company.id)
Now, if you wish to test same in your template, you can add is_user_admin in your context, something like this:
views.py
return render(request, 'admin/users.html', {'data': data, 'is_company_admin': is_company_admin(request.user)})
Now you can evaluate you response in a template:
users.html
{% if is_company_admin %}
... do something ...
{% endif %}
Simple and clean solution, based on answers that can be found earlier in this thread, but done differently. Hope it will help someone.
Tested in Django 3.0.4.