Leon
Leon

Reputation: 6544

Check user.is_active in all views

I have many views and in most of them I need check, if the user is_active:

if request.user.is_active:
    pass # some actions
else:
    #user.logout

How can it be shorten? I use @login_required (Django 1.6.2). If I change through admin user active = no, I can do any actions in profile (is_active returns False) like a normal user. But I need reverse: user with is_active False cant do anything, for example, they automaticaly logout.

How to do this? Thanks.

Upvotes: 0

Views: 181

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55215

Use:

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_active)
def my_view(request):
    # blah

Upvotes: 1

Related Questions