Prometheus
Prometheus

Reputation: 33655

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.

Upvotes: 3

Views: 3384

Answers (4)

danncti
danncti

Reputation: 11

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'}),
        }

Upvotes: 0

Shailesh Yadav
Shailesh Yadav

Reputation: 301

You need to define PasswordInput.

class YourForm(forms.ModelForm):

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

Upvotes: 0

Ion Scerbatiuc
Ion Scerbatiuc

Reputation: 1171

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.

Upvotes: 6

lehins
lehins

Reputation: 9767

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>

Upvotes: 2

Related Questions