From 63616ae64fa3e4bc8729fbc65a07fd6f5a24506f Mon Sep 17 00:00:00 2001 From: Martino Pizzol Date: Wed, 4 Apr 2018 14:40:36 +0200 Subject: [PATCH] Add a new settings to customize the size of the boostrap grid --- README.rst | 3 +++ django_forms_bootstrap/templatetags/bootstrap_tags.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 54a43a5..bd1b80f 100644 --- a/README.rst +++ b/README.rst @@ -57,6 +57,9 @@ and include your form using something like the following markup: :: +You can also specify the number of columns in your bootstrap grid using the +Django settings `BOOTSTRAP_COLUMNS_GRID` + Specifying Form Layouts ----------------------- diff --git a/django_forms_bootstrap/templatetags/bootstrap_tags.py b/django_forms_bootstrap/templatetags/bootstrap_tags.py index d4325d3..c66f71a 100644 --- a/django_forms_bootstrap/templatetags/bootstrap_tags.py +++ b/django_forms_bootstrap/templatetags/bootstrap_tags.py @@ -1,4 +1,5 @@ from django import template +from django.conf import settings from django.template.loader import get_template from django import VERSION as DJANGO_VERSION @@ -62,6 +63,7 @@ def as_bootstrap_inline(form): def as_bootstrap_horizontal(form, label_classes=""): template = get_template("bootstrap/form.html") form = _preprocess_fields(form) + grid_columns = getattr(settings, "BOOTSTRAP_COLUMNS_GRID", 12) if label_classes == "": label_classes = "col-md-2" @@ -71,19 +73,18 @@ def as_bootstrap_horizontal(form, label_classes=""): "single_container": "", "wrap": "", } - for label_class in label_classes.split(" "): split_class, column_count = label_class.rsplit("-", 1) column_count = int(column_count) - if column_count < 12: + if column_count <= grid_columns: offset_class = "{split_class}-offset-{column_count}".format( split_class=split_class, column_count=column_count, ) wrap_class = "{split_class}-{column_count}".format( split_class=split_class, - column_count=12 - column_count, + column_count=grid_columns - column_count, ) css_classes["single_container"] += offset_class + " " + wrap_class + " " css_classes["wrap"] += wrap_class + " "