Django:如何检查用户是否将所有字段留空(或初始值)?

时间:2021-03-03 11:46:00

I know is_valid() on a bounded form checks if all required data is entered. This is not what I want. I want to check if any field was filled on the form.

我知道有界表单上的is_valid()检查是否输入了所有必需的数据。这不是我想要的。我想检查表单上是否填写了任何字段。

Any ideas?

有任何想法吗?

Elaborating:

阐述:

I want to give the user the choice of not filling in the form at all. However, if they attempt to fill it in (ie: changed a value in a field from it's initial value---usually empty value), I want to validate it.

我想让用户选择不填写表格。但是,如果他们试图填写它(即:从一个字段中更改一个值的初始值---通常为空值),我想验证它。

The view would be something like this:

视图将是这样的:

def get_opinion(request):

    if request.method == 'POST':
        f = OpinionForm(request.POST)
        if form_is_blank(f):
            return HttpResponseRedirect(reverse('thank_you_anyway'))
        elif f.is_valid():
            #Process here
            return HttpResponseRedirect(reverse('thanks_for_participating'))
    else:
        initial = {'weekday': date.today().strftime('%A')}
        f = OpinionForm(initial=initial)

    return render_to_response(
        'get_opinion.html',
        {'form': f,},
        RequestContext(request)
    )

What I want is the form_is_blank() part.

我想要的是form_is_blank()部分。

4 个解决方案

#1


23  

Guess I have to answer my own question.

我猜我必须回答自己的问题。

Apparently, there's an undocumented Form function: has_changed()

显然,有一个未记录的Form函数:has_changed()

>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({'name': 'test'})
>>> f.has_changed()
True
>>> f = MyForm({'name': 'test'}, initial={'name': 'test'})
>>> f.has_changed()
False

So this would do nicely as the replacement for form_is_blank() (reverted of course).

所以这可以很好地替换form_is_blank()(当然还原)。

#2


4  

To get this functionality work for subset of Forms used in the actual <form> tag you also need to define

要使此功能适用于实际

标记中使用的表单子集,您还需要定义

class YourForm(forms.ModelForm):
    def full_clean(self):
        if not self.has_changed():
            self._errors = ErrorDict()
            return

        return super(YourForm, self).full_clean()

so when the user is prompted to fix validation errors it doesnt display errors from the forms which you want to validate only if some value isn't blank (or default).

因此,当提示用户修复验证错误时,仅当某些值不为空(或默认值)时,才会显示要验证的表单中的错误。

#3


3  

You should set required=False for all fields in the form and override the clean method to do custom validation. See the current documentation for more.

您应该为表单中的所有字段设置required = False,并覆盖clean方法以进行自定义验证。有关更多信息,请参阅当前文档

#4


0  

If you have put required=True in your forms field or in the model blank=False so is_valid() should return False.

如果您在表单字段中或在模型中将required = True设置为空= False,则is_valid()应返回False。

#1


23  

Guess I have to answer my own question.

我猜我必须回答自己的问题。

Apparently, there's an undocumented Form function: has_changed()

显然,有一个未记录的Form函数:has_changed()

>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({'name': 'test'})
>>> f.has_changed()
True
>>> f = MyForm({'name': 'test'}, initial={'name': 'test'})
>>> f.has_changed()
False

So this would do nicely as the replacement for form_is_blank() (reverted of course).

所以这可以很好地替换form_is_blank()(当然还原)。

#2


4  

To get this functionality work for subset of Forms used in the actual <form> tag you also need to define

要使此功能适用于实际

标记中使用的表单子集,您还需要定义

class YourForm(forms.ModelForm):
    def full_clean(self):
        if not self.has_changed():
            self._errors = ErrorDict()
            return

        return super(YourForm, self).full_clean()

so when the user is prompted to fix validation errors it doesnt display errors from the forms which you want to validate only if some value isn't blank (or default).

因此,当提示用户修复验证错误时,仅当某些值不为空(或默认值)时,才会显示要验证的表单中的错误。

#3


3  

You should set required=False for all fields in the form and override the clean method to do custom validation. See the current documentation for more.

您应该为表单中的所有字段设置required = False,并覆盖clean方法以进行自定义验证。有关更多信息,请参阅当前文档

#4


0  

If you have put required=True in your forms field or in the model blank=False so is_valid() should return False.

如果您在表单字段中或在模型中将required = True设置为空= False,则is_valid()应返回False。