0

验证单个字段是没有问题的。self.cleaned_data 显示值!

但是当我想在clean 方法中打印 self.cleaned_data 时,所有值的结果都是 None !这将如何发生?我还尝试使用 Super() 获取数据……但没有运气……有什么想法吗?

def validator_number_or_one_letter(fieldvalue):
    if not re.match("^[A-Za-z0-9]*$", fieldvalue) or len(fieldvalue)>1:

        raise forms.ValidationError("Error, Error!")


class CodeForm(forms.Form):

    letter_one = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1','class':'first-field' }))
    letter_two =forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))
    letter_three = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#', 'requiried':'required','required': 'true','size':'1' }))
    letter_four = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))
    letter_five = forms.CharField(widget=forms.TextInput(attrs={'placeholder': '#','requiried':'required','required': 'true','size':'1' }))

    def clean(self):
        #cleaned_data = super(CodeForm, self).clean()  ###this does not work either
        #print cleaned_data ###this does not work either
        print self.cleaned_data['letter_one']

    def clean_letter_one(self):
        validator_number_or_one_letter(self.cleaned_data['letter_one'])

    def clean_letter_two(self):
        validator_number_or_one_letter(self.cleaned_data['letter_two'])

    def clean_letter_three(self):
        validator_number_or_one_letter(self.cleaned_data['letter_three'])

    def clean_letter_four(self):
        validator_number_or_one_letter(self.cleaned_data['letter_four'])

    def clean_letter_five(self):
        validator_number_or_one_letter(self.cleaned_data['letter_five']) 
4

1 回答 1

0

不确定这是否是唯一的问题,但您的 clean_() 方法必须返回清理后的值。

于 2013-03-09T00:23:34.903 回答