【问题标题】:customize the way usercreationform looks in django自定义 usercreationform 在 django 中的外观
【发布时间】:2014-03-01 14:46:23
【问题描述】:

我想从 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'}),
}

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

【问题讨论】:

  • 您确定已包含form assets 吗?您可以发布您正在使用的完整模板吗?
  • 为什么我需要表单资源,我在模板中包含了 css 和 js,但小部件没有获取类。资产不是用来定义css和js的吗?
  • 我误解了 CSS 类没有被渲染。您是否尝试使用列出表单字段的声明性表单?

标签: django django-forms


【解决方案1】:

当我面临同样的问题时,我偶然发现了这一点。您可以只覆盖 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'

【讨论】:

    【解决方案2】:

    您应该覆盖 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
    

    【讨论】:

    • 每个变量值之前 _ 的含义是什么?我的 python 和 django 版本不喜欢那样(python 2.7 django 1.5)
    • from django.utils.translation import ugettext_lazy as _ 这会自动将字符串添加到翻译中
    【解决方案3】:

    您需要从 sctratch 创建表单,它不应扩展 UserCreationForm。 UserCreationForm 具有明确定义的用户名字段以及其他一些字段。你可以看看here

    【讨论】:

    【解决方案4】:
    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
            }
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 2018-06-11
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2011-08-25
      相关资源
      最近更新 更多