Skip to main content

Django Markdown to HTML Field

A Django model field for handling Markdown content with advanced sanitization and extensions support.

This field converts Markdown to HTML on the model instance save and stores converted HMTL to a specific text field.

Features

  • Markdown field for Django models
  • Automatic conversion to HTML, which will be stored in TextField
  • Built-in HTML sanitization
  • Support for superscript (^text^) and subscript (text) syntax
  • Configurable HTML tag and attribute whitelisting
  • Automatic handling of external links with nofollow
  • Lazy loading for images
  • Multi-language URL handling
  • Support for django-modeltranslation

Installation

pip install django-markdown-html-field

Usage

Register in settings.py

INSTALLED_APPS = [
    ...
    'django_markdown_html_field'
]

Add MarkdownField to your model

from django_markdown_html_field.fields import MarkdownField

class Page(models.Model):
    content = models.TextField(verbose_name='Content', blank=True, null=True)
    content_markdown = MarkdownField(verbose_name='Content - Markdown', html_field='content')

In this example, the Markdown content will be stored in the content_markdown field and the HTML will be stored in the content field.

Conversion will be executed automatically on the model instance save.

Configuration of classes for html tags

You can configure the classes for html tags in the settings.py file.

MARKDOWN_FIELD_SANITIZER_CONFIG = {
    'img': ('img-fluid', 'd-block', 'mx-auto', 'mw-100', 'mvh-75'),
    'table': ('table', 'table-bordered', 'table-hover')
}

Customize the sanitizer and worker

You can inherit from the sanitizer and worker and implement your own logic.

After that you can register your custom classes in the settings.py file.

For example:

MARKDOWN_FIELD_WORKER = 'your_app.markdown.DoFollowMarkdownWorker'
MARKDOWN_FIELD_SANITIZER = 'your_app.markdown.DoFollowHtmlSanitizer'

Example of custom worker and sanitizer

from django_markdown_html_field.worker import MarkdownWorker
from django_markdown_html_field.sanitizer import HtmlSanitizer


class DoFollowHtmlSanitizer(HtmlSanitizer):

    def _add_rel_attr(self, tag, attr):
        if self.kwargs.get('dofollow', False):
            return
        super()._add_rel_attr(tag, attr)


class DoFollowMarkdownWorker(MarkdownWorker):

    def __init__(self, text, instance=None, html_sanitizer=None, **kwargs):
        super().__init__(text=text, instance=instance, html_sanitizer=html_sanitizer, **kwargs)
        if instance:
            self.kwargs.update({'dofollow': getattr(instance, 'dofollow', False)})

Using Custom worker and sanitizer in specific model only

from django_markdown_html_field.fields import MarkdownField
from your_app.markdown import DoFollowMarkdownWorker, DoFollowHtmlSanitizer

class Page(models.Model):
    content = models.TextField(verbose_name='Content', blank=True, null=True)
    content_markdown = MarkdownField(
        verbose_name='Content - Markdown', 
        html_field='content',
        markdown_worker=DoFollowMarkdownWorker,
        html_sanitizer=DoFollowHtmlSanitizer,
    )

Using MarkdownWorker separately

from django.views import View
from django.http import JsonResponse
from django_markdown_html_field.sanitizer import HtmlSanitizer
from django_markdown_html_field.worker import MarkdownWorker

class MarkdownView(View):
    """
    Markdown view for preview html content
    """
    def post(self, request):
        return JsonResponse({'preview': MarkdownWorker(request.POST.get('content'), HtmlSanitizer).generate_html()})

Metadata

Release files for django-markdown-html-field 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-markdown-html-field 1.0.2
File Size Uploaded Provenance Actions
django_markdown_html_field-1.0.2.tar.gz 8.1 kB Without provenance Details for django_markdown_html_field-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-markdown-html-field 1.0.2
File Interpreter ABI Platform Actions
django_markdown_html_field-1.0.2-py3-none-any.whl Python 3 none any Details for django_markdown_html_field-1.0.2-py3-none-any.whl

Total release size: 16.6 kB

Release files / django_markdown_html_field-1.0.2.tar.gz

Download URL django_markdown_html_field-1.0.2.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
64093dad6ba238a5c95a330ce881d811c6333452a9ce42f936aafb6c0a760b74
BLAKE2b-256 checksum
How to use checksums
8f6b47ae9cc4ca0813d574a5ba791b5511bee3d684f3334d8929d75a30563342
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / django_markdown_html_field-1.0.2-py3-none-any.whl

Download URL django_markdown_html_field-1.0.2-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae9dbef9b396363b3ce44a16d6f37704f3ca0f68b1bf4dbdc3016452290a92fa
BLAKE2b-256 checksum
How to use checksums
b797d75956a1b94b8e501f3d2a666a5b54eba61c945bf93dc6d8bb5703004886
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

Security for django-markdown-html-field 1.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page