Django integration with for nh3, Python binding to Ammonia HTML sanitizer Rust crate.
Requirements
Python 3.10 to 3.13 supported.
Django 3.2 to 5.1 supported.
Installation
pip install django-nh3
Usage
project/settings.py
INSTALLED_APPS = [
# ...
"django_nh3",
# ...
]
def custom_attributes_filter(tag: str, attribute: str, value: str) -> str | None:
return value
NH3_ALLOWED_ATTRIBUTES = {
"h1": {"class"},
"h2": {"class"},
"h3": {"class"},
} # default: {}
NH3_ALLOWED_ATTRIBUTES_FILTER = custom_attributes_filter # default: None
NH3_CLEAN_CONTENT_TAGS = {"style"} # default: set()
NH3_LINK_REL = "noopener" # default: ""
NH3_STRIP_COMMENTS = True # default: False
NH3_ALLOWED_TAGS = {"h1", "h2", "h3"} # default: set()
apps/your_app/models.py
from django.db import models
from django.forms import ModelForm
from django.utils.safestring import SafeString
from django_nh3.models import Nh3Field
# get settings from settings.py
class YourModel(models.Model):
# ...
content = Nh3Field()
# ...
def your_attributes_filter(tag: str, attribute: str, value: str) -> str | None:
if attribute == "class":
return "custom-class"
return None
# set custom settings
class YourModelCustom(models.Model):
# ...
content = Nh3Field(
attributes={"h1": {"class"}, "h2": {"class"}, "h3": {"class"}},
attribute_filter=your_attributes_filter,
clean_content_tags={"style"},
link_rel="stylesheet",
strip_comments=True,
tags={"h1", "h2", "h3"},
)
# ...
Contributing
The project is in it’s infancy, setup because of bleach becoming deprecated.
It is setup with pre-commit to maintain code quality. This includes black for formatting, ruff for linting & checks. This is much like django, so currently referring to django’s own style docs will be most helpful
To contribute, fork the repo and clone your fork to your machine. Then setup a virtual environment however you prefer to do this. Then run the following setup the basics:
python -m pip install pre-commit tox-uv
pre-commit install
Tox is used to run tests locally and on github. The test requirements are generated using pip-tools via requirements/compile.py.
Metadata
Release files for django-nh3 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_nh3-0.3.1.tar.gz | 16.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_nh3-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.3 kB
Release files / django_nh3-0.3.1.tar.gz
| Download URL | django_nh3-0.3.1.tar.gz |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5682981d5aeff0577ada7414a57e7a0fd1f59a2ca682c5408d0b197aec8d5d80
|
|
BLAKE2b-256 checksum How to use checksums |
73898a38f9325e794de02b386e5596bc14921a67484a6c15c82946ff3a104d35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency logRelease files / django_nh3-0.3.1-py3-none-any.whl
| Download URL | django_nh3-0.3.1-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
858444153cc3073a741951fdf7b108785d2a267fc98dc1401b6e7206fc13e524
|
|
BLAKE2b-256 checksum How to use checksums |
11dd65a4ff54541caec25cc794efa2835bfab8cd5fb12beb8f6349a6d238f370
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency log