【问题标题】:django crispy forms - how to group the fields?django 酥脆的表格 - 如何对字段进行分组?
【发布时间】:2014-06-11 19:04:11
【问题描述】:

我的表格在这里:

class SortFieldsForm(forms.Form):
    LatestyearModel=forms.BooleanField(label="latest year")
    LowestPrice=forms.BooleanField(label="lowest price")
    HighestPrice=forms.BooleanField(label="highest price")
    Newest_Entry=forms.BooleanField(label="latest date")

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_id = 'id-exampleForm'
        self.helper.form_class = 'form-inline'
        self.helper.form_method = 'post'
        self.helper.form_action = 'sort_posting'

        #self.helper.add_input(Submit('submit', 'Submit'))
        super(SortFieldsForm, self).__init__(*args, **kwargs)

        self.helper.layout = Layout(
            #Fieldset(
            #    'price',
            #    'LowestPrice',
            #    'HighestPrice',
             ),
            PrependedText('LowestPrice', ''),
            PrependedText('HighestPrice', ''),
            PrependedText('Newest_Entry', ''),
            PrependedText('LatestyearModel', ''),
            ButtonHolder(
                Submit('submit', 'Submit', css_class='button white')
            )
        )

目前表格显示如下:

我想将最低价格和最高价格组合到一个单选中, 最新日期和最近年份复选框与字段旁边的提交按钮整齐对齐。不像现在那样低于他们。

任何指针/提示?

【问题讨论】:

    标签: django django-forms django-crispy-forms


    【解决方案1】:

    您应该将ChoiceFieldMultipleChoiceField 与标签和自定义小部件RadioSelectCheckboxSelectMultiple 一起使用。

    更多解释

    首先,一些代码。我说的是CheckboxSelectMultiple,但你可以不用它。单选按钮实际上需要RadioSelect。您可以使用 Bootstrap 水平表单布局在标签后放置复选框和单选按钮。

    class SortFieldsForm(forms.Form):
        price_order=forms.ChoiceField(widget=forms.RadioSelect, choices=(('lowest', 'Lowest first'), ('highest',  'Highest first')))
        newest_entry=forms.BooleanField(label="Latest date")
        latest_year=forms.BooleanField(label="Latest year")
    
        helper = FormHelper()
        helper.form_class = 'form-horizontal'
        helper.label_class = 'col-lg-2'
        helper.field_class = 'col-lg-8'
        helper.layout = Layout(
            'price_order',
            'newest_entry',
            'latest_year',
            Submit('submit', 'Submit', css_class='button white'))
    

    尝试这样做。我不能保证没有错别字,但您可以通过这种方式实现您的目标。

    另外,请查看以下链接:

    了解引导程序及其类。

    【讨论】:

    • 请提供更多解释
    • @eagertoLearn 提供
    • 谢谢乔治,我现在就测试一下!
    【解决方案2】:

    试试这个:

    class SortFieldsForm(forms.Form):
        LatestyearModel=forms.BooleanField(label="latest year")
        LowestPrice=forms.BooleanField(label="lowest price")
        HighestPrice=forms.BooleanField(label="highest price")
        Newest_Entry=forms.BooleanField(label="latest date")
    
        def __init__(self, *args, **kwargs):
            self.helper = FormHelper()
            self.helper.form_id = 'id-exampleForm'
            self.helper.form_class = 'form-inline'
            self.helper.form_method = 'post'
            self.helper.form_action = 'sort_posting'
    
            #self.helper.add_input(Submit('submit', 'Submit'))
            super(SortFieldsForm, self).__init__(*args, **kwargs)
    
            self.helper.layout = Layout(
    
                'LowestPrice',
                'HighestPrice',
                'Newest_Entry',
                'LatestyearModel' ,
                ButtonHolder(
                    Submit('submit', 'Submit', css_class='button white')
                )
            )
    

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 1970-01-01
      • 2018-11-22
      • 2016-05-18
      • 2013-04-23
      • 2020-06-13
      • 2021-07-07
      • 2016-01-29
      • 2017-09-13
      相关资源
      最近更新 更多