A Django middleware for rendering Markdown responses.
Project description
A Django middleware that converts HTML responses to Markdown when the client sends an Accept: text/markdown request header.
Requirements
django-markdown-middleware supports Python 3.11+ and requires Django 5.2 or later.
Installation
$ pip install django-markdown-middleware
Add the middleware to your Django settings:
MIDDLEWARE = [
...
"markdown_middleware.middleware.MarkdownMiddleware",
...
]
The middleware must be placed after any middleware that sets the response content type (e.g. after CommonMiddleware).
Usage
Any client that sends Accept: text/markdown in the request headers will receive the HTML response body converted to Markdown, with the response Content-Type changed to text/markdown; charset=utf-8.
The response also includes an X-Markdown-Tokens header containing an approximate token count of the converted Markdown content (estimated as len(markdown) // 4).
Only HTTP 200 responses with a text/html content type are converted. All other responses are passed through unchanged.
Caching
To enable caching of converted Markdown responses, set MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT in your Django settings to the desired cache duration in seconds:
# Cache converted Markdown responses for 5 minutes
MARKDOWN_MIDDLEWARE_CACHE_TIMEOUT = 300
When this setting is absent or None, no caching is performed.
Cache keys have the form markdown_middleware:<path>, for example markdown_middleware:/api/products/, making them easy to inspect or manage directly in your cache backend.
Cache Invalidation
To invalidate cached Markdown responses for a specific path, use invalidate_cache. It removes all cached variants of that path, including responses with different query strings:
from markdown_middleware import invalidate_cache
# Invalidates /blog/my-post/, /blog/my-post/?page=2, etc.
invalidate_cache("/blog/my-post/")
This is useful in post-save signals or management commands when content changes and the cached Markdown must be refreshed.
Prepare for development
Install uv, then:
$ uv sync --group dev
Run the tests:
$ make tests
Project details
Release history Release notifications | RSS feed
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_markdown_middleware-0.1.0.tar.gz.
File metadata
- Download URL: django_markdown_middleware-0.1.0.tar.gz
- Upload date:
- Size: 77.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
725ab43a25c7366190c3fe8394fc7daf64e7af76fdb443c43f32775c83c0e840
|
|
| MD5 |
c15b35104aa320bbe2ce924bbd1af7cc
|
|
| BLAKE2b-256 |
5da8d9e315845626a3d96c410126e24743cb2c84b3fec1def0bd66435895363f
|
File details
Details for the file django_markdown_middleware-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_markdown_middleware-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdfb232b17ef3ffabb2a376fc9dda785a84016f0f5584065f150dd669fef6355
|
|
| MD5 |
a0c8435830411fa891ce6bb45f01b00b
|
|
| BLAKE2b-256 |
ecada78389dafac58068490891bb01463f0002d699a8f2f442c02f0eedb19f1f
|