pythondjangodjango-crispy-forms

Django crispy forms shows password as clear text


Crispy forms currently shows my password field as clear text. I have tried the following but I still cannot get it to use type=password.

Fieldset(
            'Enter a password.',
            PrependedText('password', '<i class="fa fa-key"></i>',
                          placeholder='Password',
                          autocomplete='off',
                          widget=forms.PasswordInput,
                          ),
        ),

I have also tired type="password" with no effect.

I get no error.


Solution

  • 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.