更改 Bootstrap Crispy Form 的默认类

发布于 2025-01-10 13:39:10 字数 1348 浏览 1 评论 0 原文

我正在使用 Crispy Forms 和 Bootstrap4。目前,所有输入字段均使用 form-control 类呈现。我想将 form-control-lg 添加到输入字段。

我已尝试以下操作,但它不起作用

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
        help_texts = {
            'username': None,
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password1'].help_text = None
        self.fields['password2'].help_text = None
        self.fields['password2'].label = "Confirm Password"
        self.helper = FormHelper()
        self.helper.layout = Layout(
        Field(
            'username', css_class="form-control form-control-lg my-custom-class"
            )
        )

Template

<div class="content-section">
        <form method="POST" action="">
          <h3>Sign Up</h3>
          <hr>
          {% csrf_token %}
         {{ form | crispy }}  
          </br>
          <button type="submit" class="btn btn-primary">Sign Up</button>
        </form>
 </div>

另外,我可以全局修改所有输入字段的类吗?

I am using Crispy Forms with Bootstrap4. Currently, all the input fields are rendered with form-control class. I want to add form-control-lg to the input fields.

I have tried the following but it is not working

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
        help_texts = {
            'username': None,
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password1'].help_text = None
        self.fields['password2'].help_text = None
        self.fields['password2'].label = "Confirm Password"
        self.helper = FormHelper()
        self.helper.layout = Layout(
        Field(
            'username', css_class="form-control form-control-lg my-custom-class"
            )
        )

Template

<div class="content-section">
        <form method="POST" action="">
          <h3>Sign Up</h3>
          <hr>
          {% csrf_token %}
         {{ form | crispy }}  
          </br>
          <button type="submit" class="btn btn-primary">Sign Up</button>
        </form>
 </div>

Also, can I modify the class globally for all input fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-01-17 13:39:11

遵循您已有的模式。我们先从这个方法开始:

1.

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

class Meta:
    model = User
    fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
    help_texts = {
        'username': None,
    }

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['password1'].help_text = None
    self.fields['password2'].help_text = None
    self.fields['password2'].label = "Confirm Password"
    self.helper = FormHelper()
    # Attach to helper
    self.helper.form_class = 'form-control-lg'

FormHelper 有一个可以设置的属性列表,主要影响表单属性。

<块引用>

让我们看看如何在模板中呈现表单。假设我们在模板上下文中将表单作为 example_form,我们将渲染它:

{% load crispy_forms_tags %}
{% crispy example_form example_form.helper %}

<块引用>

请注意,{% Crispy %} 标记需要两个参数:第一个是表单变量,然后是帮助程序。在本例中,我们使用附加到表单的 FormHelper,但您也可以创建一个 FormHelper 实例并将其作为上下文变量传递。大多数时候,您会想要使用附加的帮助程序。请注意,如果您命名 FormHelper 属性助手,则只需执行以下操作:

{% crispy form %}

在项目 settings.py 中添加此行

CRISPY_CLASS_CONVERTERS = {'textinput': "textinput inputtext"}

<块引用>

例如,此设置将生成

因此,从上面你可以用这种方式替换:

CRISPY_CLASS_CONVERTERS = {'input-control': "input-control-lg inputtext"}

来自 Django Crispy Form Doc 的参考和引用

编辑

检查此答案

Following the pattern you already have. Let's start with this method:

1.

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

class Meta:
    model = User
    fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
    help_texts = {
        'username': None,
    }

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['password1'].help_text = None
    self.fields['password2'].help_text = None
    self.fields['password2'].label = "Confirm Password"
    self.helper = FormHelper()
    # Attach to helper
    self.helper.form_class = 'form-control-lg'

FormHelper has a list of attributes that can be set, that affect mainly form attributes.

Let’s see how to render the form in a template. Supposing we have the form in the template context as example_form, we would render it doing:

{% load crispy_forms_tags %}
{% crispy example_form example_form.helper %}

Notice that the {% crispy %} tags expects two parameters: first the form variable and then the helper. In this case we use the FormHelper attached to the form, but you could also create a FormHelper instance and pass it as a context variable. Most of the time, you will want to use the helper attached. Note that if you name your FormHelper attribute helper you will only need to do:

{% crispy form %}

Inside project settings.py add this line

CRISPY_CLASS_CONVERTERS = {'textinput': "textinput inputtext"}

For example this setting would generate <input class"textinput inputtext" .... The key of the dictionary textinput is the Django’s default class, the value is what you want it to be substituted with, in this case we are keeping textinput.

So from above you could substitute this way:

CRISPY_CLASS_CONVERTERS = {'input-control': "input-control-lg inputtext"}

References and quotes from Django Crispy Form Doc

Edit

Check this answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文