如何在Django表单中设置密码显示/隐藏眼睛按钮

mar*_*thu 6 django django-forms python-3.x

首先,我将尝试使用原生 HTML 和 jquery 来解决这个问题,但我想以 Django 形式解决这个问题,而不需要任何脚本。

我将尝试这个源代码。

我的预期输出是将这个相同的示例放在上面的 Django 表单链接中。

小智 2

class CustomerLoginFrom(ModelForm):
    class Meta:
        model = UserLogin
        fields = ['user_name','password']
        labels = {
            "user_name": "*Username",
            "password": "*Password"
        }    
        widgets = {
            "user_name":  TextInput(attrs={'placeholder':'ex:test','autocomplete': 'off'}), 
            "password": PasswordInput(attrs={'placeholder':'********','autocomplete': 'off','data-toggle': 'password'}),
        }
Run Code Online (Sandbox Code Playgroud)

<input type="password" id="password" name="password" class="form-control" data-toggle="password">

Django 密码字段将 HTML 属性直接添加到 Django 表单 attrs,它对我有用。