From 9b86ff149dad998fd37d85f4f2eb52fbb2ce54f4 Mon Sep 17 00:00:00 2001 From: blag Date: Tue, 19 Sep 2023 18:08:20 -0700 Subject: [PATCH 1/2] Add default_layout setting --- docs/settings.rst | 6 +++++- src/django_bootstrap5/core.py | 1 + src/django_bootstrap5/renderers.py | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 3d4d9894..3ee427ff 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -9,7 +9,7 @@ They can be modified by adding a dict variable called ``BOOTSTRAP5`` in your ``s The ``BOOTSTRAP5`` dict variable contains these settings and defaults: -.. code:: django +.. code:: python # Default settings BOOTSTRAP5 = { @@ -37,6 +37,10 @@ The ``BOOTSTRAP5`` dict variable contains these settings and defaults: # Put JavaScript in the HEAD section of the HTML document (only relevant if you use bootstrap5.html). 'javascript_in_head': False, + # Default layout class + # Can be floating, horizontal, or inline + 'default_layout': '', + # Wrapper class for non-inline fields. # The default value "mb-3" is the spacing as used by Bootstrap 5 example code. 'wrapper_class': 'mb-3', diff --git a/src/django_bootstrap5/core.py b/src/django_bootstrap5/core.py index 6852254d..a3fde632 100644 --- a/src/django_bootstrap5/core.py +++ b/src/django_bootstrap5/core.py @@ -16,6 +16,7 @@ }, "theme_url": None, "javascript_in_head": False, + "default_layout": "", "wrapper_class": "mb-3", "inline_wrapper_class": "", "horizontal_label_class": "col-sm-2", diff --git a/src/django_bootstrap5/renderers.py b/src/django_bootstrap5/renderers.py index 0a3f2d95..4a9b11a2 100644 --- a/src/django_bootstrap5/renderers.py +++ b/src/django_bootstrap5/renderers.py @@ -31,7 +31,7 @@ class BaseRenderer: form_errors_template = "django_bootstrap5/form_errors.html" def __init__(self, **kwargs): - self.layout = kwargs.get("layout", "") + self.layout = kwargs.get("layout", get_bootstrap_setting("default_layout")) self.wrapper_class = kwargs.get("wrapper_class", get_bootstrap_setting("wrapper_class")) self.inline_wrapper_class = kwargs.get("inline_wrapper_class", get_bootstrap_setting("inline_wrapper_class")) self.field_class = kwargs.get("field_class", "") From 1bb1b93d88c0cdb65da533eb940531adc0a66328 Mon Sep 17 00:00:00 2001 From: blag Date: Tue, 19 Sep 2023 19:13:24 -0700 Subject: [PATCH 2/2] Add tests --- tests/test_bootstrap_form.py | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_bootstrap_form.py b/tests/test_bootstrap_form.py index 9bacda8c..461614e2 100644 --- a/tests/test_bootstrap_form.py +++ b/tests/test_bootstrap_form.py @@ -1,6 +1,7 @@ from bs4 import BeautifulSoup from django import forms from django.forms import formset_factory +from django.test import override_settings from tests.base import BootstrapTestCase @@ -187,3 +188,77 @@ def test_horizontal_field_offset_class(self): "" ), ) + + +class DefaultLayoutTestCase(BootstrapTestCase): + @override_settings( + BOOTSTRAP5={ + "default_layout": "horizontal", + }, + ) + def test_horizontal_default_layout(self): + form = ShowLabelTestForm() + html = self.render( + "{% bootstrap_form form %}", + context={"form": form}, + ) + self.assertHTMLEqual( + html, + ( + '
' + '' + '
' + '' + '
' + '
' + ), + ) + + @override_settings( + BOOTSTRAP5={ + "default_layout": "inline", + "inline_wrapper_class": "custom-inline-wrapper-class", + }, + ) + def test_inline_default_layout(self): + form = ShowLabelTestForm() + html = self.render( + "{% bootstrap_form form %}", + context={"form": form}, + ) + self.assertHTMLEqual( + html, + ( + '
' + '' + '' + "
" + ), + ) + + @override_settings( + BOOTSTRAP5={ + "default_layout": "floating", + }, + ) + def test_floating_default_layout(self): + form = ShowLabelTestForm() + html = self.render( + "{% bootstrap_form form %}", + context={"form": form}, + ) + self.assertHTMLEqual( + html, + ( + '
' + '' + '' + '
' + ), + )