【问题标题】:validating white space,empty space and integer验证空白、空白和整数
【发布时间】:2013-05-18 08:17:18
【问题描述】:

如何在单个方法中验证空格、空格和整数。考虑我的 forms.py

class UserprofileForm(forms.ModelForm):
    class Meta:
        model = Userprofile
        fields=['username1','phonenumber1','username1','phonenumber1']

如何验证这一点。

  • 所有字段都不是强制性的。
  • 但是如果输入了 username1 或 username2 而没有分别输入 phonenumber1 或 phonenumber2 则会引发验证错误。
  • 如果输入任何空格,也会引发验证错误。
  • 是否可以在views.py 中使用strip() 来验证空白。

谁能告诉我如何做到这一点。请给我一个如何执行的例子。

谢谢

【问题讨论】:

  • 您有没有尝试过任何不起作用的方法?让人们为你做你的工作并不是 StackOverflow 的精神。
  • 我正在学习 django,我用谷歌搜索并获得了一些关于上述所有内容的验证链接,但我不知道如何在一个方法中应用所有内容。为每个字段编写单独的 clean_field() 方法将需要超过 35 行。我期望的一切都应该发生在一个 clean() 方法中。所以代码看起来会很好。我只是期待各位专家的帮助。
  • 两个答案似乎都是正确的,除了空格验证,那么如何以相同的方法验证空格。

标签: django django-models django-forms django-views


【解决方案1】:

您可以使用 clean() 方法在其中执行验证逻辑:

class UserprofileForm(forms.ModelForm):
    class Meta:
        model = Userprofile
        fields=['username1','phonenumber1','username1','phonenumber1']

    def clean(self):
        # do your validation here, such as
        cleaned_data = super(UserprofileForm, self).clean()
        username1 = cleaned_data.get("username1")
        username2 = cleaned_data.get("username2")
        phonenumber1 = cleaned_data.get("phonenumber1")
        phonenumber2 = cleaned_data.get("phonenumber2")
        if (
            ((username1 and not username1.isspace()) and not phonenumber1) or
            ((username2 and not username2.isspace()) and not phonenumber2) or
            ((not username1 or username1.isspace()) and phonenumber1 is not None) or
            ((not username2 or username2.isspace()) and phonenumber2 is not None)
        ):
            raise forms.ValidationError("Name and phone number required.")
        return cleaned_data

你可以参考Django Doc:

https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

【讨论】:

  • 如何验证空白,是否可以用同样的方法和这个逻辑
  • 您可以使用 (username1 and username1.isspace()) 来验证空白。
  • crossin,我按照你的回答,很抱歉没有发生验证。我尝试了一些代码,因为我不知道如何实现空白,所以你能告诉我如何如果我更新我尝试过的代码,请验证空格。
  • if ((usernam1 and not username1.isspace()) and not (phonenumber1 and not phonenumber1.isspace())) or ((usernam2 and not username2.isspace()) and not (phonenumber2 and not phonenumber2.isspace())): raise forms.ValidationError
  • 我试过这个得到这个错误“'int'对象没有属性'isspace'”
【解决方案2】:
class UserprofileForm(forms.ModelForm):
    class Meta:
        model = Userprofile
        fields=['username1','phonenumber1','username2','phonenumber2']

    def clean(self):
        if 'username1' in self.cleaned_data and 'phonenumber1' in self.cleaned_data:
            if not (self.cleaned_data['username1'] and self.cleaned_data['phonenumber1']):
                raise forms.ValidationError("You must enter both username1 and phonenumber1")
        if 'username2' in self.cleaned_data and 'phonenumber2' in self.cleaned_data:
            if not (self.cleaned_data['username2'] and self.cleaned_data['phonenumber2']):
                raise forms.ValidationError("You must enter both username2 and phonenumber2")


        return self.cleaned_data

您可以检查此验证方法。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多