Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default layout setting #531

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/django_bootstrap5/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down
75 changes: 75 additions & 0 deletions tests/test_bootstrap_form.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -210,3 +211,77 @@ def test_horizontal_field_offset_class(self):
"</div>"
),
)


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,
(
'<div class="mb-3 row">'
'<label class="col-form-label col-sm-2" for="id_subject">'
'Subject'
'</label>'
'<div class="col-sm-10">'
'<input class="form-control" id="id_subject" name="subject" placeholder="Subject" required type="text">'
'</div>'
'</div>'
),
)

@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,
(
'<div class="col-12 custom-inline-wrapper-class">'
'<label class="visually-hidden" for="id_subject">'
'Subject'
'</label>'
'<input class="form-control" id="id_subject" name="subject" placeholder="Subject" required type="text">'
"</div>"
),
)

@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,
(
'<div class="form-floating mb-3">'
'<input class="form-control" id="id_subject" name="subject" placeholder="Subject" required type="text">'
'<label class="form-label" for="id_subject">'
'Subject'
'</label>'
'</div>'
),
)