python 清除表单验证错误时的 Django 表单字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:25:19  来源:igfitidea点击:

Clearing Django form fields on form validation error?

pythondjangodjango-forms

提问by ChrisW

I have a Django form that allows a user to change their password. I find it confusing on form error for the fields to have the *'ed out data still in them.

我有一个允许用户更改密码的 Django 表单。我发现字段中仍然存在 *'ed out 数据的表单错误令人困惑。

I've tried several methods for removing form.data, but I keep getting a This QueryDict instance is immutableexception message.

我已经尝试了几种删除 form.data 的方法,但我不断收到This QueryDict instance is immutable异常消息。

Is there a proper way to clear individual form fields or the entire form data set from clean()?

是否有正确的方法来清除单个表单字段或整个表单数据集clean()

采纳答案by Igor Sobreira

If you need extra validation using more than one field from a form, override the .clean() method. But if it's just for one field, you can create a clean_field_name() method.

如果您需要使用表单中的多个字段进行额外验证,请覆盖 .clean() 方法。但如果只是针对一个字段,则可以创建一个 clean_field_name() 方法。

http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation

http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation

回答by Ian Clelland

Regarding the immutable QueryDict error, your problem is almost certainly that you have created your form instance like this:

关于不可变的 QueryDict 错误,您的问题几乎可以肯定是您已经像这样创建了表单实例:

form = MyForm(request.POST)

This means that form.data isthe actual QueryDict created from the POST vars. Since the request itself is immutable, you get an error when you try to change anything in it. In this case, saying

这意味着 form.data从 POST 变量创建的实际 QueryDict。由于请求本身是不可变的,因此当您尝试更改其中的任何内容时会出现错误。在这种情况下,说

form.data['field'] = None

is exactly the same thing as

完全一样

request.POST['field'] = None

To get yourself a form that you can modify, you want to construct it like this:

要获得一个可以修改的表单,您需要像这样构造它:

form = MyForm(request.POST.copy())

回答by ChrisW

Someone showed me how to do this. This method is working for me:

有人向我展示了如何做到这一点。这种方法对我有用:

post_vars = {}  
post_vars.update(request.POST)  
form = MyForm(post_vars, auto_id='my-form-%s')  
form.data['fieldname'] = ''  
form.data['fieldname2'] = ''

回答by LaundroMat

Can't you just delete the password data from the form's cleaned_data during validation?

你不能在验证过程中从表单的cleaned_data中删除密码数据吗?

See the Django docs for custom validation(especially the second block of code).

有关自定义验证(尤其是第二个代码块),请参阅Django 文档

回答by Farhan Hafeez

I just created the form again.

我刚刚再次创建了表单。

Just try:

你试一试:

form = AwesomeForm()

and then render it.

然后渲染它。

回答by Visgean Skeloru

Django has a widget that does that: https://docs.djangoproject.com/en/1.8/ref/forms/widgets/#passwordinput

Django 有一个小部件可以做到这一点:https: //docs.djangoproject.com/en/1.8/ref/forms/widgets/#passwordinput

now if you are not working with passwords you can do something like this:

现在,如果您不使用密码,则可以执行以下操作:

class NonceInput(Input):
    """
    Hidden Input that resets its value after invalid data is entered
    """
    input_type = 'hidden'
    is_hidden = True

    def render(self, name, value, attrs=None):
        return super(NonceInput, self).render(name, None, attrs)

Of course you can make any django widget forget its value just by overriding its render method (instead of value I passed None in super call.)

当然,您可以通过覆盖其渲染方法(而不是我在超级调用中传递 None 的值)使任何 django 小部件忘记其值。

回答by piyer

I guess you need to use JavaScript to hide or remove the text from the container.

我猜您需要使用 JavaScript 来隐藏或删除容器中的文本。