Django Bootstrap3
django-bootstrap3 is a Python library that integrates Bootstrap version 3 into Django projects. It provides template tags and filters to render forms, messages, and other Bootstrap components easily within Django templates. The current version is 26.1, and it maintains a steady release cadence.
Common errors
-
django.template.exceptions.TemplateSyntaxError: 'bootstrap3' is not a registered tag library, or is not available. Did you go and assign it a name and not use it?
cause The `bootstrap3` template tag library was not explicitly loaded in the template.fixAdd `{% load bootstrap3 %}` as the very first line of your Django template file. -
django.core.exceptions.ImproperlyConfigured: The app 'bootstrap3' must be in INSTALLED_APPS.
cause The `bootstrap3` application is not included in your Django project's `INSTALLED_APPS` setting.fixOpen your `settings.py` file and add `'bootstrap3'` to your `INSTALLED_APPS` list. -
My Django form fields are not rendered with Bootstrap 3 styles, or form errors are not displayed correctly.
cause Either Bootstrap 3 CSS/JavaScript is not loaded, or the Django form is not being rendered using the `django-bootstrap3` template tags.fixEnsure `{% bootstrap_css %}` and `{% bootstrap_javascript %}` are present in your template's `<head>`. Use `{% bootstrap_form form %}` to render the entire form, or `{% bootstrap_field field %}` for individual fields, where `form` is your Django form instance.
Warnings
- breaking This library is exclusively for Bootstrap 3, which is an end-of-life (EOL) framework and no longer receives official updates or security patches. Using it in new projects or expecting compatibility with modern Bootstrap versions (4 or 5) is highly discouraged and will lead to broken layouts and potential security vulnerabilities.
- gotcha Forgetting to add `'bootstrap3'` to your `INSTALLED_APPS` in `settings.py` will prevent the library's template tags from being recognized by Django, leading to `ImproperlyConfigured` errors.
- gotcha Omitting `{% load bootstrap3 %}` at the beginning of your Django template will cause `TemplateSyntaxError` for all `bootstrap3`-related tags, as the tag library won't be available.
- gotcha If your forms or other components are not displaying with Bootstrap 3 styles, you might be missing `{% bootstrap_css %}` and/or `{% bootstrap_javascript %}` in your template's `<head>` section, or static files are not served correctly in production.
Install
-
pip install django-bootstrap3
Imports
- add_css_classes
from bootstrap3.utils import add_css_classes
- render_field
from bootstrap3.forms import render_field
Quickstart
{% load bootstrap3 %}
<!DOCTYPE html>
<html>
<head>
<title>Django Bootstrap3 Example</title>
{% bootstrap_css %}
{% bootstrap_javascript %}
<style>
.container { margin-top: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>Contact Us</h1>
{% bootstrap_messages %}
<form action="" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form layout='horizontal' %}
{% buttons submit='Submit' %}
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-envelope"></span> Submit
</button>
{% endbuttons %}
</form>
</div>
</body>
</html>