A simple custom field for Django that can safely render Markdown and store it in the database.
Project description
django-markdownfield 
A simple custom field for Django that can safely render Markdown and store it in the database.
Your text is stored in a MarkdownField. When the model is saved, django-markdownfield will
parse the Markdown, render it, sanitise it with nh3, and store
the result in a RenderedMarkdownField for display to end users.
django-markdownfield also bundles a minified version of the EasyMDE editor (v2.20.0) in admin views to make working with Markdown easier.
Installation
django-markdownfield can be installed from PyPI:
pip install django-markdownfield
After installation, you need to add markdownfield to INSTALLED_APPS of your Django project's settings.
INSTALLED_APPS = [
"markdownfield",
...
"django.contrib.staticfiles",
]
Usage
Implementing django-markdownfield is simple. See the below example.
from django.db import models
from markdownfield.models import MarkdownField, RenderedMarkdownField
from markdownfield.validators import VALIDATOR_STANDARD
class Page(models.Model):
text = MarkdownField(rendered_field='text_rendered', validator=VALIDATOR_STANDARD)
text_rendered = RenderedMarkdownField()
To disable the EasyMDE editor in frontend forms:
text = MarkdownField(rendered_field='text_rendered', use_editor=False)
To disable it in the Django admin:
text = MarkdownField(rendered_field='text_rendered', use_admin_editor=False)
Use in templates
To display the rendered Markdown in a template, use the RenderedMarkdownField you defined on your
model and mark it as safe with the safe filter:
{{ post.text_rendered | safe }}
Configuration
All settings are optional.
| Setting | Default | Description |
|---|---|---|
SITE_URL |
None |
Your site's base URL (e.g. "https://example.com"). Used to distinguish internal from external links. Without it, all links are treated as external. |
MARKDOWN_EXTENSIONS |
['fenced_code'] |
List of Python-Markdown extensions to enable. |
MARKDOWN_EXTENSION_CONFIGS |
{} |
Configuration for Markdown extensions. |
MARKDOWN_LINK_BLACKLIST |
[] |
List of domains whose <a> links should be stripped from output (e.g. ['spam.example.com']). The link text is preserved; only the link itself is removed. |
MARKDOWN_MARK_EXTERNAL_LINKS |
True |
When True, external links receive target="_blank" and class="external". Set to False to disable. |
Validators
django-markdownfield comes with a number of validators, which are used to process and clean the output of the Markdown engine.
VALIDATOR_STANDARD
from markdownfield.validators import VALIDATOR_STANDARD
This validator strips any tags not used by standard Markdown.
VALIDATOR_CLASSY
from markdownfield.validators import VALIDATOR_CLASSY
Like VALIDATOR_STANDARD, but also allows class on links and images, and permits data-*
attributes. Useful for creating styled buttons and enhanced links.
VALIDATOR_NULL
from markdownfield.validators import VALIDATOR_NULL
Skips sanitization entirely. Not safe for user input. Allows arbitrary HTML in Markdown input.
Creating Custom Validators
To create a custom validator, create an instance of the markdownfield.validators.Validator dataclass:
from markdownfield.validators import Validator
# allows only bold and italic text
VALIDATOR_COMMENTS = Validator(
allowed_tags={'b', 'i', 'strong', 'em'},
allowed_attrs={},
)
You can also extend the built-in tag and attribute sets:
from markdownfield.validators import Validator, MARKDOWN_TAGS, MARKDOWN_ATTRS
VALIDATOR_CLASSY = Validator(
allowed_tags=MARKDOWN_TAGS,
allowed_attrs={
**MARKDOWN_ATTRS,
'img': {'src', 'alt', 'title', 'class'},
'a': {'href', 'alt', 'title', 'name', 'class'},
},
generic_attribute_prefixes={'data-'},
)
To allow inline CSS but restrict which properties are permitted:
VALIDATOR_STYLED = Validator(
allowed_tags=MARKDOWN_TAGS,
allowed_attrs={
**MARKDOWN_ATTRS,
'*': {'id', 'style'},
},
filter_style_properties={'color', 'font-weight', 'text-align'},
)
Note: filter_style_properties has no effect unless style is included in allowed_attrs. If style is allowed but filter_style_properties is not set, all CSS properties are permitted.
To restrict permitted URL schemes (e.g. block javascript: or custom schemes):
VALIDATOR_STRICT = Validator(
allowed_tags=MARKDOWN_TAGS,
allowed_attrs=MARKDOWN_ATTRS,
url_schemes={'http', 'https', 'mailto'},
)
Migrations
If you need to migrate from TextField or CharField to MarkdownField, add a RunPython step to your migration that calls save() on every existing instance so the rendered field is populated:
from django.db import migrations
import markdownfield.models
def save_text_rendered(apps, schema_editor):
ExampleModel = apps.get_model('yourapp', 'ExampleModel')
for examplemodel in ExampleModel.objects.all():
examplemodel.save()
class Migration(migrations.Migration):
dependencies = [
('yourapp', '000X_migrate_to_markdownfield'),
]
operations = [
migrations.AddField(
model_name='yourapp',
name='text_rendered',
field=markdownfield.models.RenderedMarkdownField(default=''),
preserve_default=False,
),
migrations.AlterField(
model_name='ExampleModel',
name='text',
field=markdownfield.models.MarkdownField(rendered_field='text_rendered'),
),
migrations.RunPython(save_text_rendered),
]
License
This software is released under the MIT license.
Copyright (c) 2019-2026 Luke Rogers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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_markdownfield-0.12.0.tar.gz.
File metadata
- Download URL: django_markdownfield-0.12.0.tar.gz
- Upload date:
- Size: 790.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":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 |
498c89b821e7ffdec155a4cdaf42e8f5779c0bbe4e9129809416d825e4146e99
|
|
| MD5 |
d0935d46a4a0d759866790225141badb
|
|
| BLAKE2b-256 |
04ae478638909aefc112d311a390571ade0655b9360af1a651548a6358fc9ef1
|
File details
Details for the file django_markdownfield-0.12.0-py3-none-any.whl.
File metadata
- Download URL: django_markdownfield-0.12.0-py3-none-any.whl
- Upload date:
- Size: 748.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":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 |
b8e52e1543ea03e1f9df6a854e6016f09fa4a3b721e08707befe276ab86e2c87
|
|
| MD5 |
5bbd7180749f160c08e03b4c217af168
|
|
| BLAKE2b-256 |
dacc3f1cf2cc5304ce58af0d948abd11248617b8b857015e5615a8b773d08d50
|