Django的。如何更改内置表单的输入类

时间:2015-01-26 18:55:32

标签: django forms

我试图更改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'),

它不起作用,表格没有获得课程表格控制,任何想法?

1 个答案:

答案 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}}