【发布时间】:2017-10-02 20:27:02
【问题描述】:
我是 python 和 Django 框架的新手,我创建了一个运行良好的登录表单,但唯一的问题是在输入密码时我无法屏蔽密码。
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-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 %}
每当我尝试在views.py中添加以下代码时
password = forms.CharField(widget=PasswordInput)
我没有任何效果,如果我尝试使用 form_class 而不是表单,它不起作用,因为类型对象 'AuthenticationForm' 没有属性 'CharField'
同样在 login-page.html 我使用了默认类型设置为 text 的输入标签有没有办法将其更改为密码?正如我在表单中的字段上循环一样。
请告诉我哪里出错了。
感谢和问候, 钦梅斯瓦米
【问题讨论】:
-
实际上我确实看到了这篇文章,但它是基于 ModelForm 但我使用的是 FormView 因此我无法将它与我的问题联系起来