HBMCS
HBMCS

Reputation: 776

Check for string in queryset within a Django template

I know this should be straight-forward, but for some reasons I'm not getting the results I want.

This instruction: {{user.profile.role.all}} in my Django template outputs this:

<QuerySet [<Role: Creator>, <Role: Performer>, <Role: Venue>]>

I'd like to check if a role is within this queryset; so, for instance, if I want to check if a role 'venue' is present, according to what the documentation tells me, I should do:

{% if "Venue" in user.profile.role.all %}

Right? The above-mentioned if, though, returns false. Why is that?

Upvotes: 2

Views: 1082

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477794

The reason this does not work is because a string is something different than a Role with as name the same string.

You can pass a set of role names to the template, for example with:

context['role'] = Role.objects.filter(
    profile__user=self.request.user
).values_list('type', flat=True)

Upvotes: 1

Related Questions