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
| Name | Type | Description |
|---|---|---|
| test_func | callable | A callable that accepts a user object and returns True if the user is authorized to access the view. |
| login_url | string = None | The URL or named URL pattern where unauthorized users are redirected; defaults to settings.LOGIN_URL if not provided. |
| redirect_field_name | string = REDIRECT_FIELD_NAME | The name of the query string parameter used to store the URL the user was trying to access before being redirected to login. |
Returns
| Type | Description |
|---|---|
function | A decorator function that wraps a Django view to enforce the provided test logic. |