脆皮表格field_class无法正确申请

发布于 2025-02-07 17:45:27 字数 2639 浏览 1 评论 0原文

我有一种django形式,其中我与Bootstrap5一起使用脆皮形式。一切都还好,直到我想改变形式的布局。由于我的表单是动态构造的,因此我只想定义集合item的值,该元素在form (field_name,layout_index)下。目的是将其定义为fieldwithbutton,因为我找不到另一种方法。

为此,我在__ INT __我的表单的方法中修改了我的助手:

self.helper[item[1]] = Div(FieldWithButtons(item[0], StrictButton("Add item")), id=f'div_id_{item[0]}', css_class='mb-3 row')

这几乎以我的形式正确地渲染,我的fieldwithbutton带有div等。但是,包含我的fieldwithbutton do div的div''' t取我定义的助手的field_class,而是创建< divClass ='col-10'> ...</divclass ='col-10'&gt ;

有一个吉斯的空间消失了,把一切都弄乱了。我如何删除class ='col-10'我的DIV的一部分,并将其视为类别,或者将我的字段定义为fieldwithbutton?

如果需要,这是我的整个表格课程:

class ScriptInputForm(forms.Form):

    def __init__(self, *args, **kwargs):
        variables = kwargs.pop('variables') # All variables to render 
        variables_names = [*variables]      # under the form {'name':['type', 'description']}

        super().__init__(*args, **kwargs)

        for var in variables_names: # Dynamic creation of the fields
            values = variables[var]
            field = self.fields[var] = forms.CharField()
            field.widget.attrs['placeholder'] = values[1].title()

        self.helper = FormHelper(self)

        num = 1 # Rough part where I define the tuples ('name', 'index') of all lists in my variables
        lists = []
        for k,v in variables.items():
            if v[0]=='list':
                lists.append((k,num))
            num+=1

        for item in lists: # Part where the problem is coming from
            self.helper[item[1]] = Div(FieldWithButtons(item[0], StrictButton("Add item"))) 

        self.helper.add_input(Submit('submit', 'Submit'),)
        self.helper.label_class = 'col-2'
        self.helper.field_class = 'col-10'

        self.helper.form_action = reverse_lazy('scripts:data_input')

以及渲染的HTML:

<div> 
    <div class="mb-3 row"> 
        <label for="id_liste" class="col-form-label col-2">Liste</label>
        <divclass="col-10"> <!-- With <div class="col-10"> everything's ok -->
            <div class="input-group"> 
                <input type="text" name="liste" placeholder="Your List" class="textinput textInput form-control" id="id_liste"> 
                    <button class="btn" type="button">Add item</button> 
            </div> 
        </divclass="col-10">
    </div>
</div>

I have a Django form in which I use crispy-forms along with bootstrap5. Everything was alright until I wanted to changed the Layout of my form. As my form is constructed dynamically, I just wanted to define the value of a set element item which is under the form (field_name, layout_index). The goal was to define it as a FieldWithButton, as I couldn't find another way to do that.

To do that, I modified my helper in the __init__ method of my form :

self.helper[item[1]] = Div(FieldWithButtons(item[0], StrictButton("Add item")), id=f'div_id_{item[0]}', css_class='mb-3 row')

This is rendered nearly correctly in my form, I have the FieldWithButton with Div etc. However, the div which contains my FieldWithButton doesn't take the field_class of my helper that I defined, and instead creates a <divclass='col-10'>...</divclass='col-10'>.

There's juste a space which disappeared and messed everything up. How can I either remove the class='col-10' part of my div and put it as its class or differently define my Field as a FieldWithButton ?

Here's my whole form class if needed :

class ScriptInputForm(forms.Form):

    def __init__(self, *args, **kwargs):
        variables = kwargs.pop('variables') # All variables to render 
        variables_names = [*variables]      # under the form {'name':['type', 'description']}

        super().__init__(*args, **kwargs)

        for var in variables_names: # Dynamic creation of the fields
            values = variables[var]
            field = self.fields[var] = forms.CharField()
            field.widget.attrs['placeholder'] = values[1].title()

        self.helper = FormHelper(self)

        num = 1 # Rough part where I define the tuples ('name', 'index') of all lists in my variables
        lists = []
        for k,v in variables.items():
            if v[0]=='list':
                lists.append((k,num))
            num+=1

        for item in lists: # Part where the problem is coming from
            self.helper[item[1]] = Div(FieldWithButtons(item[0], StrictButton("Add item"))) 

        self.helper.add_input(Submit('submit', 'Submit'),)
        self.helper.label_class = 'col-2'
        self.helper.field_class = 'col-10'

        self.helper.form_action = reverse_lazy('scripts:data_input')

And the rendered HTML :

<div> 
    <div class="mb-3 row"> 
        <label for="id_liste" class="col-form-label col-2">Liste</label>
        <divclass="col-10"> <!-- With <div class="col-10"> everything's ok -->
            <div class="input-group"> 
                <input type="text" name="liste" placeholder="Your List" class="textinput textInput form-control" id="id_liste"> 
                    <button class="btn" type="button">Add item</button> 
            </div> 
        </divclass="col-10">
    </div>
</div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文