password field is showing password in plain text

[亡魂溺海] 提交于 2019-12-24 08:19:44

问题


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 PasswordInput) for each field too. However the password field which has PasswordInput shows password in plain text. How can i resolve this?

my signup page(account/signup.html)

<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
    {% csrf_token %}
     {% for field in form.visible_fields %}
     <div class="form-group">
        <label for="{{ field.id_for_label}}">{{field.label}}</label>
        {{ field.errors.0 }}
        <input type="{{field|input_type}}" name="{{ field.name }}" class="form-control" id="{{ field.id_for_label}}">
     </div>
     {% endfor %}
</form>

filters.py

from django import template
register = template.Library()

@register.filter('input_type')
def input_type(field):
    print('field',field.field.widget.__class__)
    return field.field.widget.__class__.__name__

How can i show password in dot?


回答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'



回答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.




回答3:


You can also try this:

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


来源:https://stackoverflow.com/questions/39575910/password-field-is-showing-password-in-plain-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!