Skip to main content

Render Django form fields as bootstrap input groups.

Project description

django-bootstrap-input-group

This is an add-on for django-bootstrap5 for rendering multiple django form fields as a Bootstrap input group.

Requirements

Django 3.2 or newer and django-bootstrap5.

Installation

  1. Install using pip:

    pip install django-bootstrap-input-group
    
  2. Add to INSTALLED_APPS in your settings.py:

    INSTALLED_APPS = [
        # ...
        "django_bootstrap_input_group"
        # ...
     ]
    

Preview

Check out the little demo:

git clone https://github.com/Actionb/django-bootstrap-input-group.git
cd django-bootstrap-input-group/demo
pip install -r requirements.txt
python manage.py runserver

Then visit the demo forms on http://127.0.0.1:8000/.

Preview

Usage

In your templates, load the template tag set django_bootstrap_input_group. Then you can render an input group of fields either by:

  • declaring the groups in a field_groups attribute on your form and using the bootstrap_grouped_form template tag to render that form
  • using the bootstrap_input_group template tag to render a single group

Using field_groups form attribute:

Declare field_groups on your form:

from django import forms


class ContactForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()
    notes = forms.CharField()
    
    field_groups = [('Name', ('first_name', 'last_name')), 'notes']

Then in your templates:

{% load django_bootstrap_input_group %}

{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}

<form>
    {# Render a form as a grouped form #}
    {% bootstrap_grouped_form form %}
    
    {# Or for formsets #}
    {% bootstrap_grouped_formset formset %}
</form>

field_groups contains the names of form fields in the order that they should be rendered in, with nested lists (or tuples) of names representing a group. The nested list can be either a flat list, or a two-item list, where the first item is the label for the group and the second is the list of field names:

# No groups - just field names:
field_groups = ['first_name', 'last_name', 'notes']

# first_name and last_name make up a group, but no group label is specified:
field_groups = [('first_name', 'last_name'), 'notes']

# first_name and last_name make up a group, with the group label "Name":
field_groups = [('Name', ('first_name', 'last_name')), 'notes']

If no label is provided, the label of the first field in a group is used as the label for the group.

Using the input group template tag:

Or you can just render a group of fields directly, using the bootstrap_input_group template tag:

{% load django_bootstrap5 django_bootstrap_input_group %}

<form>
    {% bootstrap_input_group form.first_name form.last_name label="Name" %}
    {% bootstrap_field form.notes %}
</form>

Template tag arguments

The bootstrap_input_group tag takes (bound) form fields as positional arguments. It also takes an optional keyword argument label which specifies the label for the group (if no label is provided, it defaults to the label of the first field in a group).
Other than that, the django_bootstrap_input_group template tags take the same arguments as their django_bootstrap5 counterparts (see here for a list of the template tags and their parameters).

CSS

This package ships with an additional css file django_bootstrap_input_group/css/bootstrap_input_group.css which includes rules that tweak the look of RadioSelectButtonGroup widgets when rendered inside an input group. The rules:

  • vertically align the label text of the options
  • remove rounded corners of label elements (where necessary)

Simply include the css file in your form's media (also: How to manage static files).

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

django-bootstrap-input-group-1.0.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-bootstrap-input-group-1.0.1.tar.gz.

File metadata

File hashes

Hashes for django-bootstrap-input-group-1.0.1.tar.gz
Algorithm Hash digest
SHA256 be9f3fa2f523d445dbd0494e912cf920e664e017e6b20dc9e07dad760469317c
MD5 3386179f9a3dfb250dff8027ca8ef053
BLAKE2b-256 5fa33489cd2abfe4720b3d33ba85e060d006853b1dd80b621d44467e76682954

See more details on using hashes here.

File details

Details for the file django_bootstrap_input_group-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bootstrap_input_group-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3105b460e78b6f8da2850fed5f878c85ad4d024bf351a76a02fb5e8df360b670
MD5 5e692138a9722f7b203398a87b68d4df
BLAKE2b-256 0cffb142df4030213768d61c2ccfaac7dde51217f23363d5e451caf7f87ffc39

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