forms.py
class password_reset(forms.Form):
password1 = forms.PasswordInput(attrs={'palceholder':' type password'})
password2 = forms.PasswordInput(attrs={'placeholder':'type password again'})
def clean(self):
data = self.cleaned_data
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 != password2:
raise forms.ValidationError('Password Mismatch')
return data
views.py
def password_resetView(request):
if request.method == 'POST':
form = password_reset(request.POST)
if form.is_valid():
password = form.cleaned_data['password1']
obj = User.objects.get(username = request.user)
obj.set_password(password)
obj.save()
form.save()
else:
form = password_reset()
return render(request,'password_reset.html',{'form':form})
模板
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit">
</form>
所以我无法在模板中呈现表单。没有错误显示,其呈现为相应的html,但未呈现表单。
答案 0 :(得分:1)
您正在使用窗口小部件而不是字段。您必须将CharField
小部件与PasswordInput
一起使用。
password1 = forms.CharField(attrs={'placeholder': 'type password'}, widget=forms.PasswordInput)
password2 = forms.CharField(attrs={'placeholder': 'type password again'}, widget=forms.PasswordInput)