How to create password input field in django

前端 未结 9 631
春和景丽
春和景丽 2020-12-28 11:19

Hi I am using the django model class with some field and a password field. Instead of displaying regular plain text I want to display password input. I created a model class

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 12:10

    Why not just create your own password field that you can use in all your models.

    from django import forms 
    
    class PasswordField(forms.CharField):
        widget = forms.PasswordInput
    
    class PasswordModelField(models.CharField):
    
        def formfield(self, **kwargs):
            defaults = {'form_class': PasswordField}
            defaults.update(kwargs)
            return super(PasswordModelField, self).formfield(**defaults)
    

    So now in your model you use

    password = PasswordModelField()
    

提交回复
热议问题