import re


def to_filename(s):
    """
    Based on `django/utils/text.py <https://github.com/django/django/blob/master/django/utils/text.py>`_. 
    Return the given string converted to a string that can be used for a clean filename. 
    Specifically, leading and trailing spaces are removed; other spaces are converted to underscores, and anything that is not a unicode alphanumeric, dash, underscore, or dot, is removed.
 
    EXAMPLE::
    
        >>> to_filename("%  A d\sbla'{-+\)(ç? ")
        'A_dsbla-ç'
    
    """
    s = re.sub(r'(?u)[^-\w. ]', '', s)
    s = s.strip().replace(' ', '_')
    return s