0

I am new to python and Django framework and I created a login form which works well but only thing is that I am not able to mask the password while it's being entered.

Views.py

class LoginFormView(FormView):
template_name = 'Login/login-page.html'
form_class = AuthenticationForm

def form_valid(self, form):
    login(self.request, form.get_user())
    return redirect('personal:index')

Login/login-page.html

{% block content %}
    <form class="form-horizontal" method="post" enctype="multipart/form-data">
      {% csrf_token %}
        {% for field in form %}
           <div class="form-group">
               <label for="{{ field.id_for_label }}" class="col-sm-2 control-label">{{field.html_name}}</label>
               <div class="col-sm-5">
                    <input class="form-control" id="{{ field.id_for_label }}" name="{{ field.html_name }}" />
               </div>
           </div>
        {{ field.errors }}
        {% endfor %}
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-success ">
            <span class="glyphicon glyphicon-log-in" aria-hidden="true"></span>  Sign in
          </button>
        </div>
      </div>
    </form>
{% endblock %}

Whenever I try to add the following code in views.py

password = forms.CharField(widget=PasswordInput)

I has no effect and if I try the same with form_class instead of forms it doesnt work as type object 'AuthenticationForm' has no attribute 'CharField'

Also in the login-page.html I have used the input tag whose type by default is set to text is there a way to change it to password ? As I have looped on fields in the form.

Kindly let me know where I am going wrong.

Thanks and Regards, Chinmay Swami

ChinmaySwami
  • 11
  • 1
  • 1
  • 2
    Possible duplicate of [How to create password input field in django](http://stackoverflow.com/questions/9324432/how-to-create-password-input-field-in-django) – Brian H. May 04 '17 at 15:07
  • Actually I did see the post but it is based on ModelForm but I am using FormView hence I wasnt able to relate it to my issue – ChinmaySwami May 04 '17 at 15:33

1 Answers1

2

Try adding this to the form:

widget=forms.TextInput(attrs={'type' : 'password'})
drew
  • 771
  • 1
  • 4
  • 11
  • Hi it didnt work. I added the following in views.py password = forms.CharField(widget=forms.TextInput(attrs={'type' : 'password'})) – ChinmaySwami May 04 '17 at 15:39
  • You are adding it to your views? Do you not have a separate forms.py file? -- I tested this piece of code out on a form of my own and it did work. Show your whole form for the password so I can see. – drew May 04 '17 at 15:46
  • Hello Andrew sorry for the delayed response I kind of developed rest of the app. Actually I didnt create a foms.py as I am using AuthenticationForm However, I do have a forms.py which I am using for registration of user and there I added the code but still it doesnt seem to work. I do suspect that the culprit is in the HTML code The type is not getting set automatically as per the field – ChinmaySwami Jul 17 '17 at 13:22
  • you should pass your form fields from your view into the template and then call the form inputs using the following syntax: {{ field.label }}: {{ field }}. You shouldn't need to have the tag in your HTML if you have created the form correctly in the view. See the following link: https://code.djangoproject.com/wiki/PasswordField – drew Sep 04 '17 at 14:46