Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create password input field in django

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

like image 873
Dar Hamid Avatar asked Feb 17 '12 07:02

Dar Hamid


People also ask

Is there a password field in Django?

The Django's Forms The above form has two inputs - a text field named username (the name attribute in the html input field is what determines the name of input field) and a password field named password - and a submit button. The form uses POST method to submit form data to server.

How does Django validate password?

validate(self, password, user=None) : validate a password. Return None if the password is valid, or raise a ValidationError with an error message if the password is not valid. You must be able to deal with user being None - if that means your validator can't run, return None for no error.

What is the purpose of password field in form?

<input> elements of type password provide a way for the user to securely enter a password. The element is presented as a one-line plain text editor control in which the text is obscured so that it cannot be read, usually by replacing each character with a symbol such as the asterisk ("*") or a dot ("•").

What are forms in Django?

Forms are basically used for taking input from the user in some manner and using that information for logical operations on databases. For example, Registering a user by taking input as his name, email, password, etc. Django maps the fields defined in Django forms into HTML input fields.


2 Answers

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 
like image 166
Burhan Khalid Avatar answered Oct 09 '22 17:10

Burhan Khalid


You need to include the following in your imports;

from django import forms 
like image 40
DrTyrsa Avatar answered Oct 09 '22 19:10

DrTyrsa