Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crispy-forms: add css class for one of the inputs

In my forms.py I have

[...]
self.helper.layout = Layout(
    Field('name'),
    Field('description'),
)
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-2 col-xs-3'
self.helper.field_class = 'col-md-10 col-xs-9'
[...]

which renders to

<div id="div_id_name" class="form-group">
    <label class="control-label col-md-2 col-xs-3 requiredField" for="id_name">
        Name
    </label
    <div class="controls col-md-10 col-xs-9">
        <input id="id_name" class="textinput textInput form-control" type="text" name="name" maxlength="20">
    </div>
</div>
<div id="div_id_description" class="form-group">
    <label class="control-label col-md-2 col-xs-3 requiredField" for="id_description">
        Description
    </label>
    <div class="controls col-md-10 col-xs-9">
        <textarea id="id_description" class="textarea form-control" rows="10" name="description" cols="40"></textarea>
    </div>
</div>

Now I would like just the name-input to be smaller, like this:

[...]
<div class="controls col-md-8 col-xs-7">
        <input id="id_name" class="textinput textInput form-control" type="text" name="name" maxlength="20">
    </div>
</div>
[...]

(How) can this be achieved in crispy-forms?

like image 339
speendo Avatar asked Jul 17 '14 19:07

speendo


People also ask

How do I add a class to crispy form?

layout = Layout( Field('name'), Field('description'), ) self. helper. form_class = 'form-horizontal' self. helper.

How do you customize crispy forms?

But, previously crispy forms has templatetags to handle specific field. one of it is {{ form. field_name|as_crispy_field }} , this example below is output of it. Other options, you can handle it using specific html selectors/attributes inside your forms widget, such as html class , id , style , or else.


1 Answers

To you helper layout:

self.helper.layout = Layout(
    Field('name'),
    Field('description'),

add css_class:

Field('field_name', css_class="controls col-md-8 col-xs-7")

Here is the docs link

like image 95
Aaron Lelevier Avatar answered Oct 20 '22 10:10

Aaron Lelevier