Skip to main content

user_passes_test

Decorator for views that checks that the user passes the given test, redirecting to the log-in page if necessary. The test should be a callable that takes the user object and returns True if the user passes.

def user_passes_test(
test_func: callable,
login_url: string = None,
redirect_field_name: string = REDIRECT_FIELD_NAME
) - > function

Decorator for views that checks that the user passes the given test, redirecting to the log-in page if necessary. The test should be a callable that takes the user object and returns True if the user passes.

Parameters

NameTypeDescription
test_funccallableA callable that accepts a user object and returns True if the user is authorized to access the view.
login_urlstring = NoneThe URL or named URL pattern where unauthorized users are redirected; defaults to settings.LOGIN_URL if not provided.
redirect_field_namestring = REDIRECT_FIELD_NAMEThe name of the query string parameter used to store the URL the user was trying to access before being redirected to login.

Returns

TypeDescription
functionA decorator function that wraps a Django view to enforce the provided test logic.