{"id":9665,"library":"django-bleach","title":"django-bleach","description":"django-bleach is a Django package that provides easy integration of the bleach HTML sanitization library with Django models and templates. It offers model fields and template filters to clean user-supplied HTML, preventing XSS vulnerabilities. The current version is 3.1.0, with a release cadence that generally follows Django and `bleach` updates, releasing new major versions for significant dependency bumps or framework compatibility changes.","status":"active","version":"3.1.0","language":"en","source_language":"en","source_url":"https://github.com/marksweb/django-bleach","tags":["django","bleach","html-sanitizer","security","xss"],"install":[{"cmd":"pip install django-bleach","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Framework integration for Django applications.","package":"Django","optional":false},{"reason":"Core HTML sanitization library.","package":"bleach","optional":false}],"imports":[{"symbol":"BleachHTMLField","correct":"from django_bleach.models import BleachHTMLField"},{"note":"The model field is in `django_bleach.models`, not `django_bleach.fields`.","wrong":"from django_bleach.fields import BleachField","symbol":"BleachField (Model)","correct":"from django_bleach.models import BleachField"},{"note":"The form field `BleachField` is distinct from the model field and is imported from `django_bleach.forms`.","wrong":"from django_bleach.models import BleachField","symbol":"BleachField (Form)","correct":"from django_bleach.forms import BleachField"}],"quickstart":{"code":"import os\nfrom django.db import models\nfrom django_bleach.models import BleachHTMLField\n\n# Configure settings (e.g., in settings.py or test setup)\n# You can also pass these directly to the field constructor\n# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')\n# os.environ['BLEACH_ALLOWED_TAGS'] = \"['p', 'a', 'strong', 'em']\"\n# os.environ['BLEACH_ALLOWED_ATTRIBUTES'] = \"{'a': ['href', 'title']}\"\n\nclass Article(models.Model):\n    title = models.CharField(max_length=200)\n    # Use BleachHTMLField for content that might contain HTML\n    content = BleachHTMLField(\n        blank=True,\n        null=True,\n        # Field-specific allowed tags and attributes override global settings\n        tags=['p', 'a', 'h1', 'h2', 'strong', 'em', 'img'],\n        attributes={'a': ['href', 'title'], 'img': ['alt', 'src']},\n        strip_tags=False, # Do not strip tags not explicitly allowed (default is False)\n        strip_comments=True # Strip HTML comments (default is True)\n    )\n\n    def __str__(self):\n        return self.title\n\n# Example usage (after creating and migrating the model):\n# article = Article.objects.create(title='My Article', content='<h1>Hello</h1><p>This is <strong>safe</strong> content.</p><script>alert(\"XSS!\")</script>')\n# print(article.content) # Script tag should be removed.","lang":"python","description":"This quickstart demonstrates how to define a `BleachHTMLField` in a Django model. This field will automatically sanitize any HTML input based on the `tags` and `attributes` provided, or global settings configured in `settings.py`. It's crucial to explicitly define what HTML elements and attributes are permitted to prevent Cross-Site Scripting (XSS) vulnerabilities. You can define global settings like `BLEACH_ALLOWED_TAGS`, `BLEACH_ALLOWED_ATTRIBUTES` in your `settings.py` file."},"warnings":[{"fix":"Review `bleach` 5.x release notes (on PyPI or GitHub) for any changes relevant to your existing `django-bleach` configuration. Adjust `BLEACH_ALLOWED_TAGS`, `BLEACH_ALLOWED_ATTRIBUTES`, or custom logic in `settings.py` or field definitions as needed.","message":"`django-bleach` version 3.0.0 updated its core `bleach` dependency to require `bleach>=5.0.0`. If you are upgrading from an older `django-bleach` version, ensure your existing `bleach` configurations (e.g., `BLEACH_ALLOWED_TAGS`, `BLEACH_ALLOWED_ATTRIBUTES`, or custom callbacks) are compatible with `bleach` 5.x, as `bleach` itself may have breaking changes or behavioral differences.","severity":"breaking","affected_versions":"3.0.0+"},{"fix":"Before upgrading to `django-bleach` 2.x or later, ensure your project's Python environment is 3.8 or newer, and your Django version is 3.2 or newer. Upgrade your project's dependencies accordingly.","message":"`django-bleach` version 2.0.0 dropped support for older Python and Django versions. Specifically, it now requires Python `>=3.8` and Django `>=3.2`. Attempting to install or run `django-bleach` 2.x or later on incompatible environments will result in errors.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Always explicitly define `BLEACH_ALLOWED_TAGS` and `BLEACH_ALLOWED_ATTRIBUTES` in your `settings.py` for global defaults. For fields requiring different rules, override these settings directly in the `BleachHTMLField` or `BleachField` constructor. Test your sanitization thoroughly to ensure both functionality and security.","message":"Improper configuration of allowed HTML tags and attributes can lead to either over-stripping of desired HTML content or insufficient sanitization, potentially introducing security vulnerabilities (e.g., XSS). The default global settings might not be appropriate for all use cases, and field-specific overrides are critical.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"For model fields, use `from django_bleach.models import BleachField` or `from django_bleach.models import BleachHTMLField`. For form fields, use `from django_bleach.forms import BleachField`.","cause":"Attempting to import the `BleachField` model field from an incorrect or outdated module path. The model field `BleachField` and `BleachHTMLField` are located in `django_bleach.models`, while the form field `BleachField` is in `django_bleach.forms`.","error":"ImportError: cannot import name 'BleachField' from 'django_bleach.fields'"},{"fix":"Verify that all desired HTML tags are listed in `BLEACH_ALLOWED_TAGS`. For attributes, ensure they are explicitly listed for their respective tags in `BLEACH_ALLOWED_ATTRIBUTES` (e.g., `{'a': ['href', 'title']}`). If using a `BleachHTMLField`, check the `tags` and `attributes` arguments passed to its constructor.","cause":"The `BLEACH_ALLOWED_TAGS` or `BLEACH_ALLOWED_ATTRIBUTES` settings (either global in `settings.py` or field-specific) do not include the tags or attributes you expect to be retained. By default, `bleach` strips anything not explicitly allowed.","error":"HTML content is unexpectedly stripped, or certain tags/attributes are removed."}]}