60

I want to create password as password field in views.

models.py:

class User(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=50)

forms.py:

class UserForm(ModelForm):
    class Meta:
        model = User
Steffo
  • 305
  • 5
  • 13
Hemanth S R
  • 1,115
  • 2
  • 16
  • 27
  • here, this will answer your question http://stackoverflow.com/a/3715382/997562 – Viren Rajput Jul 08 '13 at 09:39
  • Possible duplicate of [How to create password input field in django](https://stackoverflow.com/questions/9324432/how-to-create-password-input-field-in-django) – ThunderBird Jun 06 '18 at 14:48
  • 50 is too small for modern hashing algorithms and long salt. I think you need and least 256 – sluge Jun 30 '21 at 14:08

4 Answers4

63

Use widget as PasswordInput

from django import forms
class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model = User
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45
  • 4
    I tried this but the password no longer renders in the fields. the field always looks empty – Freaktor Mar 18 '16 at 19:33
  • working for me on (based) createView but not updateView : I can see the dots of the "blurred" field on createView. But like @Freator, field remains empty even with initial overriding on updateView – Abpostman1 Aug 25 '22 at 09:19
41

You should create a ModelForm (docs), which has a field that uses the PasswordInput widget from the forms library.

It would look like this:

models.py

from django import models
class User(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=50)

forms.py (not views.py)

from django import forms
class UserForm(forms.ModelForm):
    class Meta:
        model = User
        widgets = {
        'password': forms.PasswordInput(),
    }

For more about using forms in a view, see this section of the docs.

Brian Dant
  • 4,029
  • 6
  • 33
  • 47
13

See my code which may help you. models.py

from django.db import models

class Customer(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=100)
    password = models.CharField(max_length=100)
    instrument_purchase = models.CharField(max_length=100)
    house_no = models.CharField(max_length=100)
    address_line1 = models.CharField(max_length=100)
    address_line2 = models.CharField(max_length=100)
    telephone = models.CharField(max_length=100)
    zip_code = models.CharField(max_length=20)
    state = models.CharField(max_length=100)
    country = models.CharField(max_length=100)

    def __str__(self):
        return self.name

forms.py

from django import forms
from models import *

class CustomerForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = Customer
        fields = ('name', 'email', 'password', 'instrument_purchase', 'house_no', 'address_line1', 'address_line2', 'telephone', 'zip_code', 'state', 'country')
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
Rama Krishna
  • 275
  • 3
  • 12
1

I thinks it is vary helpful way.

models.py

from django.db import models
class User(models.Model):
    user_name = models.CharField(max_length=100)
    password = models.CharField(max_length=32)

forms.py

from django import forms
from Admin.models import *
class User_forms(forms.ModelForm):
    class Meta:
        model= User
        fields=[
           'user_name',
           'password'
            ]
       widgets = {
      'password': forms.PasswordInput()
         }