【问题标题】:Django Widgets with Bootstrap带有 Bootstrap 的 Django 小部件
【发布时间】:2021-08-31 04:18:57
【问题描述】:

我正在尝试使用引导程序对我的表单进行风格化。如您所知,bootstrap 使用很多类来完成它的工作。通过谷歌搜索,我发现在我的表单中注入了一些新类,我可以将小部件与 django 一起使用。我的表格如下:

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password1'].label = 'Password'
        self.fields['password2'].label = 'Password Confirmation'
        self.fields['first_name'].label = 'First Name'
        self.fields['last_name'].label = 'Last Name'

        self.fields['password1'].help_text = None
        self.fields['password2'].help_text = None

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2' )
        help_texts = {
            'username': None,
        }
        widgets = {
            'username': forms.TextInput(
                attrs={
                    'class': 'form-control'
                }
            ),
            'first_name': forms.TextInput(
                attrs={
                    'class': 'form-control'
                }
            ),
            'last_name': forms.TextInput(
                attrs={
                    'class': 'form-control'
                }
            ),
            'email': forms.EmailInput(
                attrs={
                    'class': 'form-control'
                }
            ),
            'password1': forms.PasswordInput(
                attrs={
                    'class': 'form-control'
                }
            ),
            'password2': forms.PasswordInput(
                attrs={
                    'class': 'form-control'
                }
            )
        }

但由于某种原因,这只是将类应用于 HTML 中的用户名字段。它不会将“表单控制”类应用于其他任何东西。这是我忽略的一些简单格式问题还是我做错了什么?非常感谢任何帮助!

【问题讨论】:

    标签: python django bootstrap-4 django-widget


    【解决方案1】:

    Meta.widgets 仅适用于自动创建的表单域。如果您手动创建其中一些,则需要在此处指定您的小部件。

    class SignUpForm(UserCreationForm):
        first_name = forms.CharField(max_length=30, widget=forms.TextInput(
            attrs={
                'class': 'form-control'
            }
        ))
        last_name = forms.CharField(max_length=30, widget=forms.TextInput(
            attrs={
                'class': 'form-control'
            }
        ))
        email = forms.EmailField(widget=forms.EmailInput(
            attrs={
                'class': 'form-control'
            }
        ))
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['password1'].label = 'Password'
            self.fields['password2'].label = 'Password Confirmation'
            self.fields['first_name'].label = 'First Name'
            self.fields['last_name'].label = 'Last Name'
    
            self.fields['password1'].help_text = None
            self.fields['password2'].help_text = None
    
        class Meta:
            model = User
            fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2' )
            help_texts = {
                'username': None,
            }
            widgets = {
                'username': forms.TextInput(
                    attrs={
                        'class': 'form-control'
                    }
                ),
                'password1': forms.PasswordInput(
                    attrs={
                        'class': 'form-control'
                    }
                ),
                'password2': forms.PasswordInput(
                    attrs={
                        'class': 'form-control'
                    }
                )
            }
    

    这仅解释了first_namelast_nameemail 的情况。对于password1password2,情况实际上是相同的,但有点模糊,因为这些字段是在您的父类UserCreationForm 中手动定义的。您需要再次定义它们以更改小部件。或者,您可以覆盖表单类的 __init__ 中的小部件。

    【讨论】:

    • 哦,哇,谢谢你提供的信息,我不知道,因为我对 django 还是很陌生。我肯定会尝试所有这些,因为您提到密码可能会更困难,因为它们是手动定义的。如果我自己无法弄清楚,我会尝试通过更多的谷歌搜索来弄清楚!
    • 实际上我在回复中忘记提及的快速问题,当您说我需要再次定义它们以更改小部件时,您到底是什么意思?因为我正在使用 django 基于 UserCreationForm 为我创建表单,并且还基于 django 创建的 User 模型。
    • 好吧,我还设法通过覆盖我的表单类的 init 来实现您提到的另一种方式,如下所示:self.fields['password1'].widget = forms.widgets.PasswordInput(attrs={'class': 'form-control'}) self.fields['password2'].widget = forms.widgets.PasswordInput(attrs={'class': 'form-control'}) 抱歉格式我不确定如何cmets中的邮政编码。对于编程来说相对较新,使用这种 init 覆盖方式有什么负面影响吗?这会被认为是不好的做法吗?
    猜你喜欢
    • 2012-02-14
    • 2016-03-13
    • 2013-01-14
    • 2021-08-22
    • 2019-05-29
    • 2013-04-27
    • 1970-01-01
    • 2017-01-08
    • 2019-04-19
    相关资源
    最近更新 更多