Automatically trim newforms text fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def make_trimming(field):
    def clean_hook(self, old_clean, value):
        result = old_clean(value)
        if isinstance(result, (str, unicode,)):
            result = result.strip()
        return result
    old_clean = field.clean
    import new
    field.clean = new.instancemethod(
        lambda self, value: clean_hook(self, old_clean, value),
        field, field.__class__)

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.