自定义usercreationform在django中的外观

自定义usercreationform在django中的外观,django,django-forms,Django,Django Forms,我想从django自定义UserCreationForm。我做以下几点 class myUserCreationForm(UserCreationForm): class Meta: model=User fields = ('username', 'password1', 'password2') widgets = { 'username':TextInput(attrs={'class':'form-con

我想从django自定义UserCreationForm。我做以下几点

class myUserCreationForm(UserCreationForm):


    class Meta:
        model=User
        fields = ('username', 'password1', 'password2')
        widgets = {
            'username':TextInput(attrs={'class':'form-control'}),
            'password':TextInput(attrs={'class':'form-control'}),
            'password2':TextInput(attrs={'class':'form-control'}),
}

但它不起作用。当呈现模板时,它创建的输入框没有附加表单控件类。可能有什么问题?

您需要从策略创建表单,它不应该扩展UserCreationForm。UserCreationForm中有一个明确定义的用户名字段以及一些其他字段。您可以查看它。

您应该覆盖Meta类上面的字段。这对我很有用:

class CustomCreateUserForm(UserCreationForm):

username = forms.RegexField(
    label=_("Login"), max_length=30, regex=r"^[\w.@+-]+$",
    help_text=_("Required. 30 characters or fewer. Letters, digits and "
                "@/./+/-/_ only."),
    error_messages={
        'invalid': _("This value may contain only letters, numbers and "
                     "@/./+/-/_ characters.")},
    widget=TextInput(attrs={'class': 'form-control',
                            'required': 'true',
                            'placeholder': 'Login'
    })
)

password1 = forms.CharField(
    label=_("Password"),
    widget=forms.PasswordInput(attrs={'class': 'form-control',
                                      'required': 'true',

    })
)
password2 = forms.CharField(
    label=_("Password confirmation"),
    widget=forms.PasswordInput(attrs={'class': 'form-control',
                                      'type': 'password',
                                      'required': 'true',
    }),
    help_text=_("Enter the same password as above, for verification.")
)

first_name = forms.CharField(
    label=_("Name"),
    widget=forms.TextInput(attrs={'class': 'form-control',
                                  'type': 'text',
                                  'required': 'true',
    }),
    help_text=_("Enter user first and last name.")
)

email = forms.CharField(
    label=_("Email"),
    widget=forms.TextInput(attrs={'class': 'form-control',
                                  'type': 'email',
                                  'placeholder': 'Email address',
                                  'required': 'true'
    })
)

class Meta:
        model = User

当我面对同样的问题时,我偶然发现了这一点。您只需覆盖myUserCreationForm类上的init方法即可设置表单的属性

class myUserCreationForm(UserCreationForm):

class Meta:
    model=User
    fields = ('username', 'password1', 'password2')

def __init__(self, *args, **kwargs):
    super(myUserCreationForm, self).__init__(*args, **kwargs)

    self.fields['username'].widget.attrs['class'] = 'form-control'
    self.fields['password1'].widget.attrs['class'] = 'form-control'
    self.fields['password2'].widget.attrs['class'] = 'form-control'

你确定你已经包括了这个吗?你可以发布你正在使用的完整模板吗?为什么我需要表单资产,我在模板中包含css和js,但是小部件没有得到类。资产不是用来定义css和js的吗?我误解了css类没有得到渲染。您是否尝试使用列有表单字段的声明性表单?如果我在自定义表单的uu init_u_u中的用户名字段中添加了一个小部件,是否有效?我只需要添加一个小部件。我需要从头开始写一个表单吗?看看每个变量值前的u的含义是什么?我的python和django版本不喜欢django.utils.translation中的python 2.7 django 1.5,导入ugettext_lazy as,这会自动向翻译添加字符串
class myUserCreationForm(UserCreationForm):

    password1 = forms.CharField(
        label='Password',
        widget=forms.PasswordInput(attrs={'class': 'form-control'})
    )
    password2 = forms.CharField(
        label='Password',
        widget=forms.PasswordInput(attrs={'class': 'form-control'})
    )

    class Meta:
        model = User
        fields = ('username', 'password1', 'password2')
        widgets = {
            'username': TextInput(attrs={'class': 'form-control'}),
            # 'password': TextInput(attrs={'class': 'form-control'}),   # Remove This Line
            # 'password2': TextInput(attrs={'class': 'form-control'}),  # Remove This Line
        }