敏感字段的 Django 小部件

发布于 2025-01-11 22:27:43 字数 548 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文