I have already written articles on how to create a MarkdownField for Django with auto populate functionality. But now I have packaged it as a standalone package for use in Django, which I am sharing with the Django community.
You will find the project repository on GitHub: django_markdown_html_field
And you can install the package from PyPi: django-markdown-html-field
Functionality
django_markdown_html_field allows you to save both markdown text and generated HTML text in the site's database. This avoids generating HTML at runtime, improving site performance. Personally, I have been using this approach for a long time as it is the most convenient for simple sites. It also allows for flexible text handling.
In addition to generating HTML text, this package allows you to clean text from unwanted tags, styles, and scripts.
This package also supports django-modeltranslation.
Installation
Installation via pip
pip install django-markdown-html-field
Installation via poetry
poetry add django-markdown-html-field
Usage
Register the package 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, markdown text will be saved in the content_markdown field, and the generated HTML text will be automatically saved in the content field.
This is configured via the html_field argument.
Configure custom CSS classes for tags
The package supports limited configuration for the sanitizer
MARKDOWN_FIELD_SANITIZER_CONFIG = {
'img': ('img-fluid', 'd-block', 'mx-auto', 'mw-100', 'mvh-75'),
'table': ('table', 'table-bordered', 'table-hover')
}
Use custom markdown worker and sanitizer
MarkdownField has two important components:
- Worker, which converts markdown to HTML text
- Sanitizer, which performs post-processing of HTML content
You can override these components with your own
MARKDOWN_FIELD_WORKER = 'your_app.markdown.DoFollowMarkdownWorker'
MARKDOWN_FIELD_SANITIZER = 'your_app.markdown.DoFollowHtmlSanitizer'
Of course, you will have to study how these components work. But we are all developers, aren't we?
Example of custom components
By default, all links to external resources are set to nofollow, but you can override this behavior if, for example, your model instance has a dofollow field that allows dofollow links.
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)})
Use custom components for a single MarkdownField
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,
)
Use components separately from MarkdownField
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()})
Conclusion
I hope this django-markdown-html-field proves to be a useful package for you.
Once again, important links:
You will find the project repository on GitHub: django_markdown_html_field
And you can install the package from PyPi: django-markdown-html-field

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.