Bootstrap 4 for Django
django-bootstrap4 seamlessly integrates Bootstrap 4 into Django projects, providing template tags and filters for easy rendering of Bootstrap components. As of version 26.1, the library is in maintenance mode, receiving only bug fixes and security updates. This is due to Bootstrap 4 being superseded by Bootstrap 5, and new projects are encouraged to use the dedicated `django-bootstrap5` package.
Common errors
-
TemplateSyntaxError: 'bootstrap4' is not a registered tag library
cause The 'bootstrap4' app is not added to Django's INSTALLED_APPS.fixAdd `'bootstrap4'` to the `INSTALLED_APPS` list in your `settings.py` file. -
Bootstrap styles (CSS) are not applying, or JavaScript components are not working (e.g., dropdowns, modals)
cause Bootstrap CSS/JS files are not correctly loaded in the template, or JavaScript dependencies (like jQuery, Popper.js) are missing/loaded in the wrong order.fixEnsure `{% bootstrap_css %}` is in your `<head>` and `{% bootstrap_javascript %}` is placed just before the closing `</body>` tag in your base template. Check your browser's developer console for any loading errors or JavaScript conflicts. -
My forms are not rendering with Bootstrap styling even after loading bootstrap4 tags.
cause You are either not using the `{% bootstrap_form form %}` template tag, or form fields are being rendered individually without the `bootstrap_field` filter.fixTo render an entire form, use `{% bootstrap_form form %}`. For individual fields, use `{% bootstrap_field field %}` or iterate `{% for field in form %}{% bootstrap_field field %}{% endfor %}`.
Warnings
- breaking The library is in maintenance mode. No new features will be added, only bug fixes and security updates. For new projects, consider using `django-bootstrap5`.
- breaking Bootstrap 4 (and thus django-bootstrap4) does not include an icon set by default, unlike Bootstrap 3. The `bootstrap_icon` template tag was removed.
- gotcha Static assets (CSS, JavaScript) may not load in production environments (`DEBUG=False`) if Django's static files are not properly configured and collected.
- breaking Migrating from `django-bootstrap3` or a Bootstrap 3 project to `django-bootstrap4` is not fully backwards compatible and requires referring to the official Bootstrap 3 to 4 migration guide for client-side changes.
Install
-
pip install django-bootstrap4
Imports
- bootstrap4 template tags
{% load bootstrap_tags %}{% load bootstrap4 %}
Quickstart
# settings.py
INSTALLED_APPS = [
# ...
'bootstrap4',
# ...
]
# templates/base.html (or any template using Bootstrap)
{% load bootstrap4 %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% bootstrap_css %}
<title>My Django App</title>
</head>
<body>
<div class="container">
<h1>Hello, Bootstrap 4!</h1>
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
</div>
{% bootstrap_javascript %}
</body>
</html>