敏感字段的 Django 小部件
我正在开发一个 Django 网站,该网站向用户提供一个表单来存储他们的 API 密钥和 API 机密。
目前,这两个字段都显示为由 django-crispy-forms
生成的 CharField
,因此它们的 API 密钥和 API 机密都是可见的。
我想要一种行为,默认情况下,字段在未聚焦时显示 ****** 或 ●●●●●●,并在聚焦时显示内容。
我尝试将此字段设为 forms.py
中的 PasswordInput()
小部件
self.fields["my_field"].widget = forms.PasswordInput()
但是,这会带来一些问题:
- 这不是理想的行为
- 浏览器将 API 密钥和 API 机密视为实际密码,并将它们保存在浏览器级别, 该字段已获得焦点,但不显示字段的内容。要进行更改,必须重新输入整个值
创建自定义小部件是唯一的解决方案吗?
I am working on a Django website that presents users a form to store their API keys and API secrets.
At the moment both fields are showing as CharField
generated by django-crispy-forms
so both their API key and API secrets are visible.
I would like a behaviour where by default the fields are showing ****** or ●●●●●● when not focused and revealing the content when focused.
I have tried making this field a PasswordInput()
widget in forms.py
with
self.fields["my_field"].widget = forms.PasswordInput()
However this brings a couple of problems:
- Browsers consider the API keys and an API secrets as actual passwords and save them at the browser level which is not a desired behaviour
- When the field is focused, the contents of the fields are not shown. To make a change, the whole value has to be re-entered
Is creating a custom widget the only solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论