Skip to content

Commit

Permalink
Remove buttons tag (#37)
Browse files Browse the repository at this point in the history
* Remove buttons tag
* Update documentation
  • Loading branch information
dyve committed Apr 11, 2021
1 parent 5358127 commit 4ee04dd
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [0.3.0] - In development

- Remove `buttons` tag.
- Drop support for Django 3.0, extended support stopped on 2021-04-01).
- Add support for Django 3.2.

Expand Down
24 changes: 24 additions & 0 deletions MIGRATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Migration Guide

Below is a list of caveats when migrating from `django-bootstrap4` (Bootstrap 4) to `django-bootstrap5` (Bootstrap 5).

This document only considers the differences between `django-bootstrap4` and `django-bootstrap5`. For the migration
guide from Bootstrap 3 to 4, please look at the Bootstrap docs, especially the `Migration section <https://getbootstrap.com/docs/4.6/migration/>`_.

## Removed templatetags

### buttons

The `{% buttons %} ... {% endbuttons %}` tag has bene removed. To create buttons, use the `{% bootstrap_button %}` tag.

## jQuery

Bootstrap 5 does not depend on jQuery. Every function and tag referencing jQuery has been removed.

If you need jQuery, you will have to include it yourself.

## Popper

We use the bundled version of Bootstrap 5 JavaScript that includes Popper.

If you need a separate Popper.js file, do not use the `{% bootstrap_javascript %}` tag, but load the JavaScript yourself.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ The full documentation is at https://django-bootstrap5.readthedocs.io/
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>
```

Expand Down
10 changes: 5 additions & 5 deletions docs/example_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
{# Display a form #}
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
Submit
</button>
{% endbuttons %}
{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>
{# Read the documentation for more information #}
23 changes: 1 addition & 22 deletions docs/migrate.rst
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
=========
Migration
=========

Below is a list of caveats when migrating from `django-bootstrap4` (Bootstrap 4) to `django-bootstrap5` (Bootstrap 5).

This document only considers the differences between `django-bootstrap4` and `django-bootstrap5`. For the migration
guide from Bootstrap 3 to 4, please look at the Bootstrap docs, especially the `Migration section <https://getbootstrap.com/docs/4.6/migration/>`_.

jQuery
------

Bootstrap 5 does not depend on jQuery. Every function and tag referencing jQuery has been removed.

If you need jQuery, you will have to include it yourself.

Popper
------

We use the Bootstrap 5 JavaScript that has bundled Popper.

If you want a separate Popper.js file, do not use the `{% bootstrap_javascript %}` tag, but load the JavaScript yourself.
.. mdinclude:: ../MIGRATE.md
1 change: 0 additions & 1 deletion example/templates/app/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}

</form>

{% endblock %}
6 changes: 5 additions & 1 deletion example/templates/app/form_by_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

<form role="form" method="post">
{% csrf_token %}

{% bootstrap_form_errors form type='non_fields' %}

{% bootstrap_field form.subject layout='horizontal' size='sm' %}
{% bootstrap_field form.message placeholder='bonkers' %}
{% buttons submit='OK' reset="Cancel" %}{% endbuttons %}

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>

{% endblock %}
5 changes: 4 additions & 1 deletion example/templates/app/form_horizontal.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

<form role="form" class="form-horizontal" method="post">
{% csrf_token %}

{% bootstrap_form form layout="horizontal" %}
{% buttons submit='OK' reset='Cancel' layout='horizontal' %}{% endbuttons %}

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>

{% endblock %}
5 changes: 4 additions & 1 deletion example/templates/app/form_inline.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

<form class="row row-cols-lg-auto g-3 align-items-center" method="post">
{% csrf_token %}

{% bootstrap_form form layout='inline' %}
{% buttons submit='OK' reset='Cancel' layout='inline' %}{% endbuttons %}

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>

{% endblock %}
5 changes: 4 additions & 1 deletion example/templates/app/form_with_files.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

<form method="post" enctype="multipart/form-data">
{% csrf_token %}

{% bootstrap_form form layout=layout %}
{% buttons submit='OK' reset="Cancel" %}{% endbuttons %}

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>

{% endblock %}
5 changes: 4 additions & 1 deletion example/templates/app/formset.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
{% bootstrap_formset_errors form %}
<form role="form" method="post">
{% csrf_token %}

{% bootstrap_formset form %}
{% buttons submit='OK' reset="Cancel" %}{% endbuttons %}

{% bootstrap_button button_type="submit" content="OK" %}
{% bootstrap_button button_type="reset" content="Cancel" %}
</form>

{% endblock %}
64 changes: 1 addition & 63 deletions src/django_bootstrap5/templatetags/django_bootstrap5.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
from ..forms import (
render_button,
render_field,
render_field_and_label,
render_form,
render_form_errors,
render_form_group,
render_formset,
render_formset_errors,
render_label,
)
from ..html import render_link_tag, render_script_tag
from ..utils import handle_var, parse_token_contents, render_template_file, url_replace_param
from ..utils import render_template_file, url_replace_param

MESSAGE_ALERT_TYPES = {
message_constants.DEBUG: "warning",
Expand Down Expand Up @@ -614,66 +612,6 @@ def bootstrap_alert(content, alert_type="info", dismissible=True, extra_classes=
return render_alert(content, alert_type, dismissible, extra_classes)


@register.tag("buttons")
def bootstrap_buttons(parser, token):
"""
Render buttons for form.
**Tag name**::
buttons
**Parameters**::
submit
Text for a submit button
reset
Text for a reset button
**Usage**::
{% buttons %}{% endbuttons %}
**Example**::
{% buttons submit='OK' reset="Cancel" %}{% endbuttons %}
"""
kwargs = parse_token_contents(parser, token)
kwargs["nodelist"] = parser.parse(("endbuttons",))
parser.delete_first_token()
return ButtonsNode(**kwargs)


class ButtonsNode(template.Node):
def __init__(self, nodelist, args, kwargs, asvar, **kwargs2):
self.nodelist = nodelist
self.args = args
self.kwargs = kwargs
self.asvar = asvar

def render(self, context):
output_kwargs = {}
for key in self.kwargs:
output_kwargs[key] = handle_var(self.kwargs[key], context)
buttons = []
submit = output_kwargs.get("submit", None)
reset = output_kwargs.get("reset", None)
if submit:
buttons.append(bootstrap_button(submit, "submit"))
if reset:
buttons.append(bootstrap_button(reset, "reset"))
buttons = " ".join(buttons) + self.nodelist.render(context)
output_kwargs.update({"label": None, "field": buttons})
css_class = output_kwargs.pop("wrapper_class", "form-group")
output = render_form_group(render_field_and_label(**output_kwargs), css_class=css_class)
if self.asvar:
context[self.asvar] = output
return ""
else:
return output


@register.simple_tag(takes_context=True)
def bootstrap_messages(context, *args, **kwargs):
"""
Expand Down
6 changes: 0 additions & 6 deletions tests/test_bootstrap_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ def test_exclude(self):
# self.assertIn("hlabel", res)
# self.assertIn("hfield", res)

def test_buttons_tag(self):
form = TestForm()
res = render_template_with_form('{% buttons layout="horizontal" %}{% endbuttons %}', {"form": form})
self.assertIn("col-md-3", res)
self.assertIn("col-md-9", res)

def test_error_class(self):
form = TestForm({"sender": "sender"})
res = render_template_with_form("{% bootstrap_form form %}", {"form": form})
Expand Down

0 comments on commit 4ee04dd

Please sign in to comment.