Reputation: 2417
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?
Upvotes: 1
Views: 1791
Reputation: 19806
You can also try this:
<input class="form-control" type="{{ field.field.widget.input_type }}"
name="{{ field.name }}"
id="id_{{ field.name }}" >
Upvotes: 0
Reputation: 503
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.
Upvotes: 1
Reputation: 553
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'
Upvotes: 2