Django integration with for nh3, Python binding to Ammonia HTML sanitizer Rust crate.
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_nh3-0.3.0.tar.gz.
File metadata
- Download URL: django_nh3-0.3.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8803969b6e3268785a13937a22f5dbd94774dbb5ed6d63b411beaa4ced1a7ea0
|
|
| MD5 |
1960c6532a370506d2efd975c2c180ee
|
|
| BLAKE2b-256 |
b055fab288f9e5a65fbfbd2d4b7aa5e37cf8f932a80c4b216ac5f8c8ae7b3cec
|
Provenance
The following attestation bundles were made for django_nh3-0.3.0.tar.gz:
Publisher:
publish-to-live-pypi.yml on marksweb/django-nh3
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_nh3-0.3.0.tar.gz -
Subject digest:
8803969b6e3268785a13937a22f5dbd94774dbb5ed6d63b411beaa4ced1a7ea0 - Sigstore transparency entry: 896881645
- Sigstore integration time:
-
Permalink:
marksweb/django-nh3@d1a7b8075b9e41008ec88b8e3835939d6d142613 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/marksweb
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-live-pypi.yml@d1a7b8075b9e41008ec88b8e3835939d6d142613 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_nh3-0.3.0-py3-none-any.whl.
File metadata
- Download URL: django_nh3-0.3.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed5d068849b0b6cd887a89063bef9935a9891af5e184fc52c76ed4087e089e08
|
|
| MD5 |
24c5611c6a42a54c15eab4a725e7754b
|
|
| BLAKE2b-256 |
1d3fec4696c6c5e81265f736478b84ee9c0c9d79523edfbfea667ae6d96b6655
|
Provenance
The following attestation bundles were made for django_nh3-0.3.0-py3-none-any.whl:
Publisher:
publish-to-live-pypi.yml on marksweb/django-nh3
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_nh3-0.3.0-py3-none-any.whl -
Subject digest:
ed5d068849b0b6cd887a89063bef9935a9891af5e184fc52c76ed4087e089e08 - Sigstore transparency entry: 896881705
- Sigstore integration time:
-
Permalink:
marksweb/django-nh3@d1a7b8075b9e41008ec88b8e3835939d6d142613 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/marksweb
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-live-pypi.yml@d1a7b8075b9e41008ec88b8e3835939d6d142613 -
Trigger Event:
release
-
Statement type: