I have a custom template tag which verifies the user's group but when I use it as a template filter in an HTML template it is bugging out all over the place.
This is my custom template tag:
@register.filter(name='is_in_group')
def is_in_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
This is the first filter in the template - which is letting every user through (even users outside these groups):
{% if request.user|is_in_group:"food bev supervisor" or "casino supervisor" or "security supervisor" or "cage supervisor" %}
But if I change the ordering to:
{% if request.user|is_in_group:"casino supervisor" or "food bev supervisor" or "security supervisor" or "cage supervisor" %}
... the code fails (lets no users through).
If I set only one group as such:
{% if request.user|is_in_group:"food bev supervisor" %}
then the filter works correctly (but I cannot set more than one group).
Is this a bug in Django? What is the best way around this?
I was able to solve this by applying the filter to each variable as such:
{% if request.user|is_in_group:"food bev supervisor" or request.user|is_in_group:"casino supervisor" or request.user|is_in_group:"security supervisor" or request.user|is_in_group:"cage supervisor" %}
It isn't pretty or Pythonic but at least it works. The Django convention is a little odd here but after trying a lot of different ways I believe this one is the correct way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.