Question

My form is here:

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

Currently the form shows as this: enter image description here

I want to group lowest price and highest price in one radioselect, latest date and latest year as checkboxes neatly aligned with submit button next to the field. Not below them as it currently now.

Any pointers/tips?

Was it helpful?

Solution

Try this out:

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

OTHER TIPS

You should use ChoiceField and MultipleChoiceField with labels and custom widgets RadioSelect and CheckboxSelectMultiple.

More explanation

First, some code. I said about CheckboxSelectMultiple but you can do without it. RadioSelect is actually needed for radio buttons. You can place checkboxes and radio buttons after their labels using Bootstrap horizontal form layout.

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

Try to do like this. I cannot guarantee if there is no typos but you can achieve your goals this way.

Also, check out links below:

Learn about bootstrap and its classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top