Django Bootstrap Form

3.4 · active · verified Thu Apr 16

django-bootstrap-form is a Python library that provides simple template tags and filters to render Django forms and fields using Bootstrap 3 markup. It simplifies the integration of Django forms into Bootstrap-themed frontends, helping developers quickly style forms with Bootstrap classes. The current version is 3.4, and it has a stable, albeit irregular, release cadence primarily driven by Django compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to render a Django form using the `bootstrap_form` template filter, or individual fields with `bootstrap_field`. It requires loading the `bootstrap_form` template tags at the top of your template. Ensure `django_bootstrap_form` is added to your Django project's `INSTALLED_APPS` for the tags to be available. The example also includes a Bootstrap 3 CSS link for correct styling.

import os
from django import forms
from django.template import Template, Context
from django.conf import settings

# Minimal Django settings setup for standalone template rendering
# In a real Django project, this is handled by your settings.py
if not settings.configured:
    settings.configure(
        TEMPLATES=[
            {
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'DIRS': [],
                'APP_DIRS': True,
                'OPTIONS': {
                    'context_processors': [
                        'django.template.context_processors.debug',
                        'django.template.context_processors.request',
                    ],
                    'libraries': {
                        'bootstrap_form': 'django_bootstrap_form.templatetags.bootstrap_form',
                    }
                },
            }
        ],
        INSTALLED_APPS=[
            'django_bootstrap_form',
            'django.contrib.auth',
            'django.contrib.contenttypes',
        ]
    )

# Simulate a simple Django form
class MyForm(forms.Form):
    name = forms.CharField(label='Your Name', max_length=100)
    email = forms.EmailField(label='Your Email')
    message = forms.CharField(widget=forms.Textarea, label='Your Message')
    subscribe = forms.BooleanField(label='Subscribe to newsletter', required=False)

# Template string demonstrating usage
template_string = '''
{% load bootstrap_form %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Django Bootstrap Form</title>
    <!-- Bootstrap 3 CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { padding: 20px; }
        .container { max-width: 600px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Contact Us</h1>
        <form method="post">
            {% csrf_token %} <!-- Required in a real Django app -->
            {{ form|bootstrap_form }}
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>

        <hr>
        <h2>Individual Field Rendering</h2>
        <form method="post">
            {% csrf_token %}
            {% for field in form %}
                {{ field|bootstrap_field }}
            {% endfor %}
            <button type="submit" class="btn btn-success">Submit Individual</button>
        </form>
    </div>
</body>
</html>
'''

# Instantiate the form
form_instance = MyForm()

# Create a context for rendering
context = Context({'form': form_instance})

# Render the template
template = Template(template_string)
rendered_html = template.render(context)

# Print the rendered HTML (in a real Django app, this would be returned by a view)
print(rendered_html)

view raw JSON →