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 ability to generate other tags than "div" to the "Div" layout class. #880

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion crispy_forms/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwarg

class Div(LayoutObject):
"""
Layout object. It wraps fields in a <div>
Layout object. It wraps fields in a <div>, or alternatively any other tag you
specify with tag='h1'.

You can set `css_id` for a DOM id and `css_class` for a DOM class. Example::

Expand All @@ -353,6 +354,7 @@ def __init__(self, *fields, **kwargs):
if not hasattr(self, 'css_class'):
self.css_class = kwargs.pop('css_class', None)

self.tag = kwargs.pop('tag', 'div')
self.css_id = kwargs.pop('css_id', '')
self.template = kwargs.pop('template', self.template)
self.flat_attrs = flatatt(kwargs)
Expand Down
4 changes: 2 additions & 2 deletions crispy_forms/templates/bootstrap/layout/div.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
<{{div.tag}} {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
{% if div.css_class %}class="{{ div.css_class }}"{% endif %} {{ div.flat_attrs|safe }}>
{{ fields|safe }}
</div>
</{{div.tag}}>
4 changes: 2 additions & 2 deletions crispy_forms/templates/bootstrap3/layout/div.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
<{{div.tag}} {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
{% if div.css_class %}class="{{ div.css_class }}"{% endif %} {{ div.flat_attrs|safe }}>
{{ fields|safe }}
</div>
</{{div.tag}}>
4 changes: 2 additions & 2 deletions crispy_forms/templates/bootstrap4/layout/div.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
<{{div.tag}} {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
{% if div.css_class %}class="{{ div.css_class }}"{% endif %} {{ div.flat_attrs|safe }}>
{{ fields|safe }}
</div>
</{{div.tag}}>
4 changes: 2 additions & 2 deletions crispy_forms/templates/uni_form/layout/div.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
<{{div.tag}} {% if div.css_id %}id="{{ div.css_id }}"{% endif %}
{% if div.css_class %}class="{{ div.css_class }}"{% endif %} {{ div.flat_attrs|safe }}>
{{ fields|safe }}
</div>
</{{div.tag}}>
4 changes: 3 additions & 1 deletion docs/layouts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ Universal layout objects

These ones live in module ``crispy_forms.layout``. These are layout objects that are not specific to a template pack. We'll go one by one, showing usage examples:

- **Div**: It wraps fields in a ``<div>``::
- **Div**: It wraps fields in a ``<div>`` or any other tag::

Div('form_field_1', 'form_field_2', 'form_field_3', ...)

If you need a different tag than ``div`` you can specify it with the ``tag`` parameter, e.g., ``Div(HTML('title'), Div(tag='h1'))``.

**NOTE** Mainly in all layout objects you can set kwargs that will be used as HTML attributes. As ``class`` is a reserved keyword in Python, for it you will have to use ``css_class``. For example::

Div('form_field_1', style="background: white;", title="Explication title", css_class="bigdivs")
Expand Down