How to create password input field in django

Dar Hamid picture Dar Hamid · Feb 17, 2012 · Viewed 97.6k times · Source

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 like this:

class UserForm(ModelForm):
    class Meta:
        password = forms.CharField(widget=forms.PasswordInput)
        model = User
        widgets = {
            'password': forms.PasswordInput(),
        }

But i am getting the following error: NameError: name 'forms' is not defined.

I am using django version 1.4.0. I followed this link : Django password problems

Still getting the same error. What should i do. Where am i getting wrong.Please help

Answer

Burhan Khalid picture Burhan Khalid · Feb 17, 2012

The widget needs to be a function call, not a property. You were missing parenthesis.

class UserForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())
    class Meta:
        model = User