django脆皮形式 - 如何分组领域?

时间:2014-04-25 21:58:19

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

我的表格在这里:

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')
            )
        )

目前表单显示如下: enter image description here

我想在一个radioselect中分组最低价格和最高价格, 最新日期和最近一年的复选框与字段旁边的提交按钮整齐排列。不像目前那样低于它们。

任何指针/提示?

2 个答案:

答案 0 :(得分: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'))

尝试这样做。我不能保证是否没有拼写错误,但你可以通过这种方式实现目标。

另外,请查看以下链接:

了解bootstrap及其类。

答案 1 :(得分:1)

试试这个:

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')
            )
        )