API Reference¶
Public Classes¶
MarkdownEditorWidget¶
-
Django form widget. Extends
django.forms.widgets.Textarea.Parameters:
toolbar,upload_url,finalize_url,height,placeholder,attrs
MarkdownEditorAdminMixin¶
-
ModelAdmin mixin. Add before
admin.ModelAdminin the MRO.Attributes:
markdown_fields--list[str] | None.Noneapplies to all TextFields.
MarkdownCleanupMixin¶
-
Abstract model mixin. Deletes orphaned media on
save().Attributes:
markdown_cleanup_fields--list[str] | None.Nonetracks all TextFields.
BaseRenderer¶
-
Abstract base class for server-side markdown renderers.
Abstract methods:
render(markdown_text: str) -> str
DefaultRenderer¶
-
Default renderer using python-markdown with HTML sanitization. Falls back to HTML-escaped text if python-markdown is not installed.
BaseUploadHandler¶
-
Abstract base class for file upload handlers.
Methods:
validate(file: UploadedFile) -> None-- override to add validation. RaiseValidationErrorto reject.save(file: UploadedFile) -> str-- abstract. Store the file and return its URL.
DefaultUploadHandler¶
-
Default handler using Django's
default_storage. Saves to temp storage, withfinalize()to move to permanent.Methods:
save(file) -> str-- saves toTEMP_UPLOAD_PATH, returns URLfinalize(temp_url) -> str-- moves from temp toUPLOAD_PATH, returns new URL
Utility Functions¶
extract_media_urls(text: str) -> set[str]¶
-
Extract all media URLs from markdown text -- images (
), links ([](url)), and HTMLsrcattributes (<video>,<img>,<iframe>).
delete_orphaned_media(old_text: str, new_text: str) -> list[str]¶
-
Compare old and new markdown, delete files removed from content. Returns list of deleted storage paths.
get_setting(key: str) -> Any¶
-
Retrieve a setting value with fallback to defaults.
Raises
KeyErrorfor unrecognized setting names.
URL Endpoints¶
Both endpoints are registered under the django_markdown_widget app namespace. Protected by @csrf_protect and REQUIRE_AUTH.
POST /md-editor/upload¶
-
Upload a file to temp storage.
Request: multipart form with
filefield.Response:
{"url": "/media/md-editor/tmp/1234_photo.png", "name": "photo.png", "type": "image/png"}
POST /md-editor/finalize¶
-
Move referenced temp files to permanent storage.
Request body (JSON):
{"text": ""}Response:
{"replacements": {"/media/md-editor/tmp/1234_photo.png": "/media/md-editor/uploads/2026/03/1234_photo.png"}}Called automatically by the editor on form submit.
Template Tags & Filters¶
Load with {% load markdown_widget %}.
{% markdown text %}¶
- Render markdown text as sanitized HTML (simple tag).
{{ text|markdown }}¶
- Render markdown text as sanitized HTML (filter).