Skip to content Skip to sidebar Skip to footer

Password Field Is Showing Password In Plain Text

I have used django allauth for user registration and login system. I could show the form by simplifying the lines of code using for loop. I got the right field type(TextInput and P

Solution 1:

You can add class by overriding __init__ method in form class

def __init__(self, *args, **kwargs): 
    super().__init__(*args, **kwargs) 
    self.fields['password'].widget.attrs['class'] = 'form-control'

Solution 2:

The password is showing in plain text because you're assigning <input> types incorrectly, therefore not hiding passwords as <input type="password"> does.

From reading the comments, it looks like you're trying to add custom bootstrap classes to the form fields. As Anna Vracheva was saying, you can add the class to the fields using the form's __init__ method.

from django import forms

class CustomForm("""Whichever class you're inheriting from, probably ModelForm"""):
    # If you're using AllAuth, this is already defined on the form
    password = fields.CharField(widget=forms.PasswordInput) 
    # Or whatever field, for that matter

    def __init__(self, *args, **kwargs):
        super(CustomFieldForm, self).__init__(*args, **kwargs)

        # Option 1 - Assign to only password
        self.fields['password'].widget['class'] = 'form-control'

        # Option 2 - Loop over all fields and assign to all
        for field in self.fields:
            field.widget['class'] = 'form-control'

Then, instead of manually rendering HTML, let Django's Templates do that:

<!-- This -->
{{ field }}
<-- -->
<!-- Instead of this -->
<input type="{{field|input_type}}" name="{{ field.name }}" 
       class="form-control" id="{{ field.id_for_label}}">

That should fix any field rendering problems you're having while preserving your form classes.


Solution 3:

You can also try this:

<input class="form-control" type="{{ field.field.widget.input_type }}"
                   name="{{ field.name }}"
                   id="id_{{ field.name }}" >

Post a Comment for "Password Field Is Showing Password In Plain Text"