Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BootstrapRow for django-crispy-forms
class BootstrapRow(layout.Div):
"""
Puts form fields into bootstrap grid rows.
Usage:
======
Add fields by adding a tuple (field, columns) for each field (see example)
Example:
========
helper.layout = Layout(BootstrapRow(('field1', 6), ('field2', 6)),
BootstrapRow(('field3', 4), ('field4', 4), ('field5', 4)),
...
)
will result in the following markup:
...
<div class="row">
<div class="span6">
<!-- markup for field1 goes here -->
</div>
<div class="span6">
<!-- markup for field2 goes here -->
</div>
</div>
<div class="row">
<div class="span4">
<!-- markup for field3 goes here -->
</div>
<div class="span4">
<!-- markup for field4 goes here -->
</div>
<div class="span4">
<!-- markup for field5 goes here -->
</div>
</div>
"""
def __init__(self, *fields, **kwargs):
_fields = []
for field, cols in fields:
span = 'span%i' % cols
# expand the field itself to the given width by adding
# the span class directly
if hasattr(field, 'render'):
field.css_class = span
else:
field = layout.Field(field, css_class=span)
# wrap the field into a div with class="spanx"
field = layout.Div(field, css_class=span)
_fields.append(field)
self.css_class='row'
super(BootstrapRow, self).__init__(*_fields, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment