human
Complete the below
@register.filter
def sort_by(items, attr):

    ''''General sort filter - sorts by either attribute or key.'
    '''

Write me a the Python code you requested?
assistant
#Here is the Python code you requested.
    def key_func(item):
        try:
            return getattr(item, attr)
        except AttributeError:
            try:
                return item[attr]
            except TypeError:
                getattr(item, attr)
    return sorted(items, key=key_func)