django-bleach
Easily use bleach with Django models and templates
What it is and what it does
django-bleach wraps bleach to make HTML sanitization a first-class feature in Django applications. It provides a BleachField model field that automatically sanitizes HTML before saving to the database, a corresponding form field for user input, and template filters for rendering untrusted content safely. The package lets you define allowed HTML tags, attributes, and CSS styles through Django settings, then applies those rules consistently across your application.
The core problem it solves is preventing XSS attacks and malformed HTML when accepting user-generated content. Instead of storing raw HTML or stripping all markup, django-bleach preserves semantic tags while removing script tags, event handlers, and other dangerous constructs. It integrates directly into Django's ORM and form layer, so sanitization happens automatically without extra middleware or view-level logic.
Use it for:
- Blog or CMS platforms where users write posts with basic formatting but you need to prevent script injection
- Comment systems that allow limited HTML markup while blocking malicious content
- User profile descriptions or bio fields that should support simple rich text
- Converting URL-like strings to links in user-submitted text using the bleach_linkify filter
- Rendering third-party or legacy HTML content in templates without exposing your application to embedded attacks
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Integrates bleach HTML sanitization into Django models, forms, and templates to strip unsafe HTML tags and attributes from user input while preserving safe markup.
No. The repository is archived and abandoned with no maintenance since 2023-08-05. While the code is stable and currently compatible with Django 3.2–4.2, there is no path forward for security patches, Django 5.0+ support, or bleach library updates. For new projects, use an actively maintained alternative or apply bleach directly in your views. For existing projects still on supported Django versions, it remains functional but carries increasing technical debt.
Install
django-bleach on PyPI
pip
pip install django-bleachuv
uv add django-bleachpoetry
poetry add django-bleachInstalling django-bleach
Before you install
Low install friction with a pure-Python wheel. However, the repository is archived and abandoned as of 1105 days since last release, with the latest version from 2023-08-05. No active maintenance means security updates are unlikely.
License in practice
MIT license is permissive and imposes no restrictions on use, modification, or distribution in commercial or private projects.
Quickstart
pip install django-bleach
# In settings.py
INSTALLED_APPS = ['django_bleach']
BLEACH_ALLOWED_TAGS = ['p', 'b', 'i', 'u', 'em', 'strong', 'a']
# In models.py
from django_bleach.models import BleachField
class Post(models.Model):
content = BleachField()
Verify before relying
- Whether bleach itself receives active security updates and how that affects django-bleach's viability
- Compatibility with Django versions beyond 4.2 given the abandoned status
- Real-world performance impact of sanitization on high-volume user input
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — bleach, Django |
| Maintenance | abandoned — 1,105 days since the last release |
| Last repo commit | (repository archived) |
| First released | |
| Downloads | 105,784/month — #12,679 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_bleach-3.1.0-py2.py3-none-any.whl
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
bleachBleach sanitizes untrusted HTML by escaping or…
permissive · top 1,000 on PyPI
html-sanitizerSanitizes HTML fragments by enforcing an…
permissive · top 5,000 on PyPI
lxml-html-cleanCleans and sanitizes HTML by removing unwanted…
permissive · top 5,000 on PyPI
django-markdownifyA Django template filter that converts Markdown…
permissive · top 15,000 on PyPI
bleach-allowlistProvides curated allowlists of HTML tags,…
permissive · top 5,000 on PyPI
nh3nh3 sanitizes HTML by removing unsafe tags and…
permissive · top 1,000 on PyPI
django-ckeditorIntegrates CKEditor 4.18.0 rich-text editing…
permissive · top 5,000 on PyPI
types-bleachProvides type stubs for the bleach HTML…
permissive · top 5,000 on PyPI
django-bootstrap3Provides Django template tags and filters to…
permissive · top 15,000 on PyPI
django-template-partialsProvides reusable named template partials for…
permissive · top 15,000 on PyPI