Repository navigation
Releases
v0.4.3
truncatechars_html, truncatewords_html, filesizeformat and humanize's intword now render fast. Before this release they ran only 1.2–1.5x faster than stock Django. On the benchmark, {{ html|truncatechars_html:20 }} is now 15x faster than stock, truncatewords_html 13x, {{ n|intword }} 15x and {{ n|filesizeformat }} 10x.
pip install -U django-rust-templatesFaster
truncatechars_html/truncatewords_html: Django runs Python'sHTMLParserover every value. The native version reproduces the parser's behaviour, quirks included, for ordinary HTML: plain tags with attributes, and common entity references such as&, and'.filesizeformatandintword(django.contrib.humanize): integer inputs are formatted natively. Their translated unit strings are looked up once per active translation instead of on every call.
Output stays byte-identical to stock Django, on every supported Python version. Comments, <script>/<style> content, a bare < or &, unusual entities, non-integer numbers and patched module globals still run the Python code.
v0.4.2
The |localtime, |utc and |timezone filters and humanize's |ordinal now render fast. Before this release they ran at about the same speed as stock Django. On the benchmark, {{ value|timezone:"…" }} is now 9.8x faster than stock, {{ value|localtime }} 5.2x and {{ n|ordinal }} 24x.
pip install -U django-rust-templatesFaster
- Time zone filters:
localtime,utcandtimezonereturn Django'sdatetimeobject(adatetimesubclass). Printing that value, or passing it on to|date/|time, used to fall back to Python's date formatting. These values now take the same native path as plain datetimes. ordinal(django.contrib.humanize): Django looks up all ten suffix translations on every call. The native version looks up only the one it needs.
Output stays byte-identical to stock Django. Subclasses of datetimeobject, inputs to ordinal other than plain ints, and patched humanize module globals still run the Python code.
v0.4.1
{% csrf_token %} and inclusion tags render faster. On the benchmark, csrf_token_loop goes from 12.5x to 20x faster than stock Django, and inclusion_tag from 4.1x to 6.9x.
pip install -U django-rust-templatesFaster
- Context lookups by name (used by
{% csrf_token %}, inclusion tags and other tags that read a fixed key) now do one dict probe per context layer instead of two, and no longer build a new key string for every layer. - Inclusion tags: copying
csrf_tokeninto the included template's context, building that context, and starting the child render all skip several Python calls. Each further call in a loop now costs about 2.8 µs, down from 5.0 µs.
Output stays byte-identical to stock Django. Context subclasses that override get are still called as before.
v0.4.0
{% blocktranslate %}, {% static %} and humanize's naturaltime now run natively. A full page rendered through the backend with a request (benchmarks/realworld.py: context processors, forms, i18n, static files, humanize) goes from 2.6x to 3.5x faster than stock Django (2.9 ms → 2.2 ms).
pip install -U django-rust-templatesFaster
{% blocktranslate %}/{% blocktrans %}: about 6–7x (was 1.1x). Supportscount,context,with,trimmed,asvar, plural forms, and the stock fallbacks for missing variables and bad translations.{% static %}: about 10x (was 1.3x). With the defaultStaticFilesStorage, URLs are memoized per(STATIC_URL, path). Manifest and custom storages still call their ownurl()every time.- humanize
naturaltime: about 4.2x (was 1.2x), with output identical in every language Django ships.
Output stays byte-identical to stock Django. Custom tags that override these (a different do_static or handle_simple, for example) keep running their own Python code.
v0.3.3
Fix: date formats could be served from the wrong language after USE_I18N changed at runtime (for example, under override_settings).
pip install -U django-rust-templatesFixed
- Django resolves
gettextandpgettextseparately. The compiled date-format cache only checkedgettext, so after aUSE_I18Nflip, month names frompgettext_lazy(theNandEformat characters) could be rendered from a program compiled for a different language. The cache key now covers how both functions resolve.
v0.3.2
Fix: a {% partialdef %} partial fetched via Template.extra_data can now be rendered before its template's first render (previously AttributeError).
v0.3.1
Faster {% if %} comparisons against integer literals: {% if n > 90 %} inside a loop now renders about 2x faster (96 → 49 µs on the if_branch benchmark), moving it from 0.56x to ~1.1x of Jinja2, the only scenario where Jinja2 had been ahead.
pip install -U django-rust-templatesFaster
{% if var OP <int> %}(==,!=,<,<=,>,>=) on a plain integer compares natively. Bools, floats, strings, int subclasses, missing variables, callables and out-of-range ints take the unchanged general path, so output is identical.{% if %}writes the chosen branch straight into the page output instead of through a temporary buffer.
v0.3.0
The body of a bridged block tag now renders natively. Tags like {% localtime %}, {% timezone %}, {% cache %}, simple_block_tag, and third-party block tags call parser.parse(), which used to compile their body with Django's own parser, so everything inside ran at stock speed. Now the body compiles on the Rust parser.
pip install -U django-rust-templatesFaster
- Bridged block-tag bodies: wrapping a loop of aware dates in
{% localtime on %}used to render at 1.0x. It now renders 13.1x faster than stock, and asimple_block_tagaround a loop renders 14.5x faster. Compiling these templates went from 0.86x to 2–5x.
How it works
- The tag's own Python code still runs unchanged. Only its
parser.parse()call continues on the Rust parser. - The tag gets back a real
NodeList. Callingrender()renders the whole body natively. Inspecting it (get_nodes_by_type, iteration,len) sees stock-compatible nodes, and if a tag edits the list, it renders the edited list the stock way. {% block %}and{% partialdef %}inside a bridged tag keep working, including block overrides through{% extends %}.
Known gaps
{% blocktranslate %},{% static %}and humanizenaturaltimeare still Python.
Output stays byte-identical to stock Django. Django's template test suite passes 100% on 5.2 and 6.1.
Full change: #2
v0.2.0
Faster rendering for real pages: the slow paths that a page rendered the way a view does hits (backend + request, context processors, forms, i18n, aware datetimes, humanize) were mostly bridged Python. On the new end-to-end benchmark, benchmarks/realworld.py, the Rust engine went from 1.5x → 2.6x faster than stock Django.
pip install -U django-rust-templatesNew
-
RustFormRenderer: Django's defaultFORM_RENDERERbuilds its own stock template engine, so form and widget HTML never went throughRustTemplates. To render it natively too, set:FORM_RENDERER = "django_rust_templates.forms.RustFormRenderer"
It uses the same templates and produces identical output, about 2.6x faster. The Django admin change form renders in 9.6 ms with it, vs 17.0 ms on stock.
Faster
- Aware datetimes (
USE_TZ=True, Django's default): timezone conversion is native, saving ~9 µs per value. Also speeds up{% now %}. date/timefilters: the'c'format and locale-named formats (bare|date,SHORT_DATE_FORMAT,TIME_FORMAT) went from 1.3–1.6x to 8–13x.{% translate "literal" %}is native (1.2x → 7.3x), and lazy translations (gettext_lazylabels and verbose names) render 4.5x faster.striptagsis native for ordinary HTML (1.4x → 28x). Anything unusual still uses Django's ownstrip_tags.- humanize
intcommaon integers: 1.7x → 10.9x, with locale grouping and separators. {% url %}: 6.8 → 4.4 µs.
Fixes
copy.deepcopy()of a nativeEnginenow works. Before, deep-copyingform.errorsfailed when forms rendered throughRustTemplates.- The active language is no longer read stale after Django swaps out its translation state, which happens on settings changes and when the dev server's autoreloader reloads translation files.
- Language lookups now follow Django's own
trans_real/trans_nullchoice exactly.
Known gaps
- The body of a bridged block tag (
{% localtime %},{% cache %}, third-party block tags) still renders at stock speed. {% blocktranslate %},{% static %}and humanizenaturaltimeare still Python.
Output stays byte-identical to stock Django. Django's template test suite passes 100% on 5.2 and 6.1, and Django's forms, admin, i18n, humanize and timezones test apps also pass when run through the Rust engine.
Full change: #1
v0.1.0
First public release of django-rust-templates, a drop-in replacement for Django's template engine, written in Rust.
TEMPLATES = [{"BACKEND": "django_rust_templates.RustTemplates", ...}]- Re-implements the Django template language natively: lexer, parser, renderer, and the built-in tags and filters.
- Byte-identical output to stock Django, and 100% of Django's own template test suite passes on Django 5.2 and 6.1.
- About 14x faster rendering and template compilation on the project benchmark (
benchmarks/bench.py). - Custom and third-party tag libraries keep working:
simple_tagandinclusion_tagrun natively, and other tags are bridged to their Python code. - Prebuilt wheels for CPython 3.12–3.14 on Linux x86_64 and aarch64 (manylinux and musllinux). Requires Django ≥ 5.2.
pip install django-rust-templates