Skip to main content

Django inline formsets with bootstrap

Project description

mizdb-inlines

Django inline formsets with bootstrap for the MIZDB app.

Requires django_bootstrap5.

Formsets will be rendered in a layout with two columns. One column is for the formset form, and the other column is for the delete button of a given formset form.

Installation

Install using pip:

pip install mizdb-inlines

Add to your INSTALLED_APPS in your settings.py:

INSTALLED_APPS = [
    ...,
    "mizdb_inlines",
]

Usage

Add mizdb_inlines/js/mizdb_inlines.js javascript and render the formset using the inline_formset template tag from the mizdb_inlines template tag library:

<!DOCTYPE html>
{% load static mizdb_inlines django_bootstrap5 %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Awesome Form</title>
    {{ combined_media }}
    <script src="{% static 'mizdb_inlines/js/mizdb_inlines.js' %}"></script>
    {% bootstrap_css %}
    {% bootstrap_javascript %}
</head>
<body>
<form class="container mt-3" method="post">
    {% csrf_token %}

    {% bootstrap_form form %}
    {% inline_formset formset layout="horizontal" %}
    {% bootstrap_button button_type="submit" button_class="btn-success" content="Save" %}
    {% bootstrap_button button_type="reset" button_class="btn-warning" content="Reset" %}
</form>
</body>
</html>

The template tag instantiates an InlineFormsetRenderer and returns the renderers render() output. The template tag passes all keyword arguments along to the renderer. The renderer takes an additional keyword argument add_text with which you can set the text of the add button (defaults to the verbose name of the inline formset model). For example, in a template:

{% inline_formset formset layout="horizontal" add_text="Add another delicious Topping" %}

View mixin for inline formsets

Use the InlineFormsetMixin view mixin to remove some of the boilerplate from handling inline formsets. Simply declare the formset classes to use in the formset_classes attribute.

from mizdb_inlines.views import InlineFormsetMixin


class MyView(InlineFormsetMixin, UpdateView):
    model = Pizza
    fields = "__all__"
    template_name = "pizza.html"
    success_url = "/"

    formset_classes = (
        inlineformset_factory(Pizza, Toppings, fields="__all__", extra=1),
        MyAwesomeFormset,
    )

This will add formset instances to the template context with the context variable formsets. The combined media of the formsets and the view's model form is available with the variable combined_media :

{{ combined_media }}

{% for formset in formsets %}
    {% inline_formset formset %}
{% endfor %}

Development & Demo

python3 -m venv venv
source venv/bin/activate
make init

See the demo for a preview: run make init-demo and then start the demo server python demo/manage.py runserver.

Run tests with make test. To install required browsers for playwright: playwright install.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mizdb-inlines-0.2.4.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

mizdb_inlines-0.2.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file mizdb-inlines-0.2.4.tar.gz.

File metadata

  • Download URL: mizdb-inlines-0.2.4.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mizdb-inlines-0.2.4.tar.gz
Algorithm Hash digest
SHA256 30f11115522919beced9be03f2d0166d60f5a10d7cee1bf2c03f314dd9c50192
MD5 8e5ddcf6e35bef3234d18336e9fb8467
BLAKE2b-256 24b16c82ebbb589dd77d9030094a4e87b955c239c17aed60c3d54ba50533968b

See more details on using hashes here.

File details

Details for the file mizdb_inlines-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for mizdb_inlines-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 24a084dbc11be86162d508337e195ab914fefceecd555ac36aa97936a08bd144
MD5 fd8a8ff73d6b5149809409388373184a
BLAKE2b-256 ca1c175973d94a9f81847defacb4f51bc45090d79da895257410672e4efb637d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page