在Django的文本框中禁用自动填充? [英] Disable autocomplete on textfield in Django?

查看:746
本文介绍了在Django的文本框中禁用自动填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何关闭Django文本框中的自动填充?



例如,从我的模型生成的表单具有信用卡号的输入字段。离开自动完成是不好的做法。手工制作表单时,我会添加一个autocomplete =off语句,但是如何在Django中执行此操作,仍然保留表单验证?

解决方案

在您的表单中,指定要用于该字段的小部件,并在该小部件上添加一个 attrs 字典。例如(直接来自 django documentation ):

  class CommentForm(forms.Form):
name = forms.CharField(
widget = forms.TextInput(attrs = {'class':'special'}))
url = forms.URLField()
comment = forms.CharField(
widget = forms.TextInput attrs = {'size':'40'}))

只需添加 'autocomplete'='off' to attrs dict。


Does anyone know how you can turn off autocompletion on a textfield in Django?

For example, a form that I generate from my model has an input field for a credit card number. It is bad practice to leave autocompletion on. When making the form by hand, I'd add a autocomplete="off" statement, but how do you do it in Django and still retain the form validation?

解决方案

In your form, specify the widget you want to use for the field, and add an attrs dictionary on that widget. For example (straight from the django documentation):

class CommentForm(forms.Form):
    name = forms.CharField(
                widget=forms.TextInput(attrs={'class':'special'}))
    url = forms.URLField()
    comment = forms.CharField(
               widget=forms.TextInput(attrs={'size':'40'}))

Just add 'autocomplete'='off' to the attrs dict.

这篇关于在Django的文本框中禁用自动填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆