Skip to content

Instantly share code, notes, and snippets.

@rana-ahmed
Created July 31, 2017 10:46
Show Gist options
  • Save rana-ahmed/1fb6c4105e1c4d7e750fb9cabd9406ed to your computer and use it in GitHub Desktop.
Save rana-ahmed/1fb6c4105e1c4d7e750fb9cabd9406ed to your computer and use it in GitHub Desktop.
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
"""
Decorator for views that checks that the user is logged in, redirecting
to the log-in page if necessary.
"""
actual_decorator = user_passes_test(
lambda u: u.is_authenticated,
login_url=login_url,
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment