[Answered ]-Django – how to use user_pass_test with urls

2👍

you are missing a pair of brackets in your code example, does this work?

url (
    r'^question_detail-(?P<pk>\w+)$',
    user_passes_test(not_in_group_chef, login_url='public_connexion')(
        Question_detail.as_view()
    ),
    name='detail_question'
)

0👍

Anentropic was right, thank you !

For someone as the same problem:

The second problem that I solved is: for anonymous users, the not_in_group_chef function should verify if the user is registered. That way you won’t get an error for anonymous users.

def not_in_group_chef(user):
if user:
    retour = False
    if User.objects.filter(username = user).count()==1:
        utilisateur = User.objects.get(username = user)
        if utilisateur.groups.filter(name='chef').count()==0:
            retour = True
        return retour
    return retour
return False

Leave a comment