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
-
Install using pip:
pip install django-bootstrap-input-group
-
Add to
INSTALLED_APPS
in yoursettings.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/.
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 thebootstrap_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-bootstrap-input-group-1.0.1.tar.gz
.
File metadata
- Download URL: django-bootstrap-input-group-1.0.1.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be9f3fa2f523d445dbd0494e912cf920e664e017e6b20dc9e07dad760469317c |
|
MD5 | 3386179f9a3dfb250dff8027ca8ef053 |
|
BLAKE2b-256 | 5fa33489cd2abfe4720b3d33ba85e060d006853b1dd80b621d44467e76682954 |
File details
Details for the file django_bootstrap_input_group-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_bootstrap_input_group-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3105b460e78b6f8da2850fed5f878c85ad4d024bf351a76a02fb5e8df360b670 |
|
MD5 | 5e692138a9722f7b203398a87b68d4df |
|
BLAKE2b-256 | 0cffb142df4030213768d61c2ccfaac7dde51217f23363d5e451caf7f87ffc39 |