align field on django-crispy forms horizontally to centre of page?

3k Views Asked by At

I have a one field form which I want to align horizontally and centre of page.

I want to have autofocus and placeholder enabled.

Here is my code:

class VinForm(forms.Form):
    VIN = forms.CharField(max_length=17, label='VIN')

    def __init__(self, *args, **kwargs):
        super(VinForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_method = 'post'
        #self.helper.form_class = 'form-horizontal col-xs-12 col-md-6 col-lg-5'
        self.helper.form_class = 'form-horizontal'
        #self.helper.form_class = 'form-inline col-md-12'
        #self.helper.label_class = 'col-xs-3 col-md-3 col-lg-3'
        #self.helper.field_class = 'col-xs-12 col-md-12 col-lg-12'
        #self.helper.form_class = 'form-inline'

        self. helper.layout = Layout(
            FieldWithButtons('VIN', StrictButton("Go!"), autofocus=True, placeholder='JN1HJ01F8RT231164'),
        #     Div(
        # Div('VIN',css_class='col-md-6',autofocus=True, placeholder='JN1HJ01F8RT231164'),
        # css_class='row',
        #     ),
        #     Fieldset(
        #         #'Get Driving profile',
        #    Field('VIN', autofocus=True, placeholder='JN1HJ01F8RT231164'),
         #    ),
        #     FormActions(
        #         Submit('submit', 'get vehicle info'),
        #    ),
        # )
        )

you can see from my code above that I tried many things still none worked. Here is how it looks.

enter image description here

It lacks

1) center of page

2) inline form

3) autofocus and placeholders are not working

I am using twitter bootstrap3

2

There are 2 best solutions below

0
On

The crispy forms documentation shows how to do horizontal forms for bootstrap, perhaps that will help. http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html?highlight=horizontal#bootstrap3-horizontal-forms

0
On

I do the below. Note 'Field' in the snippet below, where we specify additional css in css_class. Here is the docs link

 def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        
        self.helper = FormHelper()
        self.helper.layout = Layout(

            Row(
                HTML('<h3>About you</h4>'),
                Column(Field('computers', css_class='text-center'), css_class='col-md-12 text-center')
            ),

https://stackoverflow.com/a/24812832/960471