django - where to clean extra whitespace from form field inputs?
Posted
by
Westerley
on Stack Overflow
See other posts from Stack Overflow
or by Westerley
Published on 2011-11-30T03:22:36Z
Indexed on
2011/11/30
9:52 UTC
Read the original article
Hit count: 148
I've just discovered that Django doesn't automatically strip out extra whitespace from form field inputs, and I think I understand the rationale ('frameworks shouldn't be altering user input').
I think I know how to remove the excess whitespace using python's re:
#data = re.sub('\A\s+|\s+\Z', '', data)
data = data.strip()
data = re.sub('\s+', ' ', data)
The question is where should I do this? Presumably this should happen in one of the form's clean stages, but which one? Ideally, I would like to clean all my fields of extra whitespace. If it should be done in the clean_field() method, that would mean I would have to have a lot of clean_field() methods that basically do the same thing, which seems like a lot of repetition.
If not the form's cleaning stages, then perhaps in the model that the form is based on?
Thanks for your help!
W.
© Stack Overflow or respective owner