[Django]-Django crispy forms shows password as clear text

6👍

I’m 99.9% positive that you missed adding the widget to the form field declaration. You should have something like this in order to display a password field:

class MyForm(forms.Form):
    ...
    password = forms.CharField(widget=forms.PasswordInput)
    ...

And the layout should be as simple as:

Layout(
    PrependedText('password', '@', placeholder="password", autocomplete='off')
)

The widget= keyword argument is defined on the Django’s forms.CharField constructor and not on the django-cryspy-forms’ bootstrap.PrependedText constructor.

You can find more information on Django forms fields here.

UPDATE

If you have a model form, it’s being constructed from your model, either automatically (eg using the admin views), by using modelform_factory or by defining the ModelForm yourself. Here is an example of the 3rd approach.

class MyModelForm(forms.ModelForm):
    class Meta:
        fields = (... fields you want to include in the form ...)

    password = forms.CharField(widget=forms.PasswordInput, ...)

Please note that by providing your model form field override, you’ll have to also provide attributes like label, help_text (and such) because they will not be automatically copied from the model field.

2👍

I never had good luck with crispy-forms and formsets, so I just use forms directly in templates. See if that works:

<form method="post">{% csrf_token %}
    {{ formset.management_form|crispy }}
    {% for form in formset %}
        <input id="id_form-{{ forloop.counter0 }}-id" name="form-{{ forloop.counter0 }}-id" type="hidden" value="{{ form.instance.pk }}"/>
        {{ form|crispy }}
    {% endfor %}
</form>
👤lehins

0👍

You need to define PasswordInput.

class YourForm(forms.ModelForm):

password = forms.CharField(label='Enter Password',widget=forms.PasswordInput(attrs={'placeholder': 'alphanumeric password'}))

0👍

Works for me: forms.py file.

    class Meta:
        model = NewUser
        fields = ('email', 'password')

        // add this to make password field 'password' type
        widgets = {
            'password': forms.PasswordInput(attrs={'type': 'password'}),
        }

Leave a comment