Reputation: 33511
When I use {{ user.groups }}
in my template, I always get auth.Group.None
, even though the user is logged in and is part of a group.
I can access the groups from the code with request.user.groups.all()
, but I need an "always works" version in my templates.
Upvotes: 0
Views: 3538
Reputation: 361
Is exactly what you just asked.
On Django the connection between User and Group is a Many2Many. So if you want to access the groups on the user you have to use
User.groups.all()
Or
User.groups.filter()
User.groups.get()
So you can realize that if you want to access group by user you need to do a queryset first.
If you want to print every group of that user you should use
{% for group in user.groups.all %}
{{ group }}
{% endfor %}
If you need to show just a specific group you should create a separated function, you will not be able to do it just with Django template.
Upvotes: 10