diff --git a/docs/settings.rst b/docs/settings.rst index 784bcd75..c99b0c86 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 = { @@ -40,6 +40,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 272f89ad..b064ea71 100644 --- a/src/django_bootstrap5/core.py +++ b/src/django_bootstrap5/core.py @@ -17,6 +17,7 @@ "theme_url": None, "color_mode": 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 c5f75593..133ccb7d 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", "") diff --git a/tests/test_bootstrap_form.py b/tests/test_bootstrap_form.py index 52e67078..f35f97cc 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 DJANGO_VERSION, BootstrapTestCase @@ -210,3 +211,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, + ( + '
' + '' + '' + '
' + ), + )