我试图更改django内置表单PassowrdChangeForm的类。这是我尝试过的,但它不起作用:
forms.py
from django.contrib.auth.forms import PasswordChangeForm
class FormChangePassword(PasswordChangeForm):
class Meta:
widgets = {
'old_password': PasswordInput(attrs={'class': 'form-control'}),
'new_password1': PasswordInput(attrs={'class': 'form-control'}),
'new_password2': PasswordInput(attrs={'class': 'form-control'})
}
如果它是有用的信息,我使用内置的password_change视图,如下所示:
urls.py
from django.contrib.auth.views import password_change
from forms import FormChangePassword
url(r'^change_password/$', password_change,
{'template_name': 'sisacademico/change_password.html',
'post_change_redirect': 'password_changed/',
'password_change_form': FormChangePassword},
name='change_password'),
它不起作用,表格没有获得课程表格控制,任何想法?
答案 0 :(得分:1)
PasswordChangeForm
不是ModelForm
,因此Meta.widgets
无效。attrs
您必须在__init__()
构造函数中设置小部件class FormChangePassword(PasswordChangeForm):
def __init__(self, *args, **kwargs):
super(FormChangePassword, self).__init__(*args, **kwargs)
for field in ('old_password', 'new_password1', 'new_password2'):
self.fields[field].widget.attrs = {'class': 'form-control'}
:
{{1}}