Python Django中输入的固定和清理表单

Python Django中输入的固定和清理表单,python,django,validation,Python,Django,Validation,Django中的clean_uu方法可能是为了允许输入表单数据的内联清理以及接受/拒绝提交而设计的 给出一个模型,其中 "FooBar" 是有效的输入。有没有一个标准的方法来解决类似的问题 "FOOBAR" "foo-bar" "FooBar " 在验证之前,以一种模糊且可概括的方式?好的,不清楚您对更改实际想做什么,但类似的方法应该可以: from re import sub ... #in your form: def clean_myfield(self): data = se

Django中的clean_uu方法可能是为了允许输入表单数据的内联清理以及接受/拒绝提交而设计的

给出一个模型,其中

"FooBar"
是有效的输入。有没有一个标准的方法来解决类似的问题

"FOOBAR"
"foo-bar"
"FooBar "

在验证之前,以一种模糊且可概括的方式?

好的,不清楚您对更改实际想做什么,但类似的方法应该可以:

from re import sub
...
#in your form:
def clean_myfield(self):
    data = self.cleaned_data['myfield']
    #Strip special chars
    data = sub("[\s_\-]","",a)
    if b.lower() != "foobar":
        raise forms.ValidationError("You have not specified foobar, you wicked boy!")
    #Do whatever conversion to camelcase you want on data (this seemed very app specific to me)
    return data
clean_字段
检查调用得相当晚(例如在字段的clean方法之后),但只要您不尝试更早地执行该验证检查,它就会正常工作


当然,如果愿意,您也可以先进行camelcase转换,然后进行相等性检查,这其实并不重要。

如果您需要从ALLCAPS转换到CapCase,简短的回答是否

您的系统如何知道AAAAA应该大写为AAAAA