code: def login_required(function=None, required=False, redirect_field_name=REDIRECT_FIELD_NAME):
      
      if required:
          if django.VERSION < (1, 11):
              actual_decorator = user_passes_test(
                  lambda u: u.is_authenticated(),
                  redirect_field_name=redirect_field_name
              )
          else:
              actual_decorator = user_passes_test(
                  lambda u: u.is_authenticated,
                  redirect_field_name=redirect_field_name
              )
          if function:
              return actual_decorator(function)
          return actual_decorator
      # login not required
      def decorator(view_func):
          def _wrapper(request, *args, **kwargs):
              return function(request, *args, **kwargs)
          return wraps(function)(_wrapper)
      return method_decorator(decorator)
docs: Decorator for views that, if required, checks that the user is logged in and redirect
      to the log-in page if necessary.