{"id":23366571,"url":"https://github.com/dmptrluke/django-markdownfield","last_synced_at":"2026-03-06T05:36:37.730Z","repository":{"id":48324587,"uuid":"226447729","full_name":"dmptrluke/django-markdownfield","owner":"dmptrluke","description":"A simple custom field for Django that can safely render Markdown and store it in the database.","archived":false,"fork":false,"pushed_at":"2025-03-07T15:53:41.000Z","size":815,"stargazers_count":45,"open_issues_count":15,"forks_count":15,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T02:01:46.607Z","etag":null,"topics":["django","markdown","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmptrluke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-07T03:07:12.000Z","updated_at":"2025-03-07T15:53:45.000Z","dependencies_parsed_at":"2024-06-19T11:26:15.295Z","dependency_job_id":"0da93bbe-7079-4800-8df3-3b658106184d","html_url":"https://github.com/dmptrluke/django-markdownfield","commit_stats":{"total_commits":54,"total_committers":8,"mean_commits":6.75,"dds":"0.33333333333333337","last_synced_commit":"18fd339f85aa743ddb65e67b5c9db5f91346040c"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmptrluke%2Fdjango-markdownfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmptrluke%2Fdjango-markdownfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmptrluke%2Fdjango-markdownfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmptrluke%2Fdjango-markdownfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmptrluke","download_url":"https://codeload.github.com/dmptrluke/django-markdownfield/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280190,"owners_count":20912966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","markdown","python"],"created_at":"2024-12-21T14:16:36.170Z","updated_at":"2026-03-06T05:36:37.031Z","avatar_url":"https://github.com/dmptrluke.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-markdownfield  [![PyPI](https://img.shields.io/pypi/v/django-markdownfield)](https://pypi.org/project/django-markdownfield/)\nA simple custom field for Django that can safely render Markdown and store it in the database.\n\nYour text is stored in a `MarkdownField`. When the model is saved, django-markdownfield will\nparse the Markdown, render it, sanitise it with [bleach](https://github.com/mozilla/bleach), and store\nthe result in a `RenderedMarkdownField` for display to end users.\n\ndjango-markdownfield also bundles a minified version of the [EasyMDE](https://github.com/Ionaru/easy-markdown-editor)\neditor (v2.14.0) in admin views to make working with Markdown easier.\n\n![alt test](https://raw.githubusercontent.com/dmptrluke/django-markdownfield/master/screenshots/editor.png)\n\n## Installation\n\ndjango-markdownfield can be installed from PyPi:\n\n```console\n# Install directly or add to your requirements.txt\npip install django-markdownfield\n```\n\nAfter installation, you need to add `markdownfield` to `INSTALLED_APPS` of your Django project's settings.\n\n```python\nINSTALLED_APPS = [\n    \"markdownfield\",\n    ...\n    \"django.contrib.staticfiles\",\n]\n```\n\n## Usage\n\nImplementing django-markdownfield is simple. See the below example.\n\n\n```python\nfrom django.db import models\n\nfrom markdownfield.models import MarkdownField, RenderedMarkdownField\nfrom markdownfield.validators import VALIDATOR_STANDARD\n\nclass Page(models.Model):\n    text = MarkdownField(rendered_field='text_rendered', validator=VALIDATOR_STANDARD)\n    text_rendered = RenderedMarkdownField()\n```\n\nPlease also set `SITE_URL` in your Django configuration - it will be needed for detecting\nexternal links.\n\n```python\nSITE_URL = \"https://example.com\"\n```\n\nTo disable the EasyMDE editor, see the amended line below.\n\n```python\ntext = MarkdownField(rendered_field='text_rendered', use_editor=False, use_admin_editor=True)\n```\n\n### Use in templates\n\nTo use the rendered markdown in templates, just use the `RenderedMarkdownField()` you created on\nyour model, like below. This field should be marked as safe with the `safe` filter to ensure it\ndisplays correctly.\n\n```djangotemplate\n{{ post.text_rendered | safe }}\n```\n\n## Validators\ndjango-markdownfield comes with a number of validators, which are used to process and clean\nthe output of the markdown engine\n\n### VALIDATOR_STANDARD\n```python\nfrom markdownfield.validators import VALIDATOR_STANDARD\n```\nThis validator strips any tags that are not used by standard Markdown. It also automatically links\nany URLs in the output, adding `class=\"external\"`, `rel=\"nofollow noopener noreferrer\"`, and\n`target=\"_blank\"` to any URLs which it determines to be external.\n\n### VALIDATOR_CLASSY\n```python\nfrom markdownfield.validators import VALIDATOR_CLASSY\n```\nThis validator does much the same as `VALIDATOR_STANDARD`, but it allows you to set the class on\nlinks and images. This is useful to create buttons and other enhanced links.\n\n### VALIDATOR_NULL\n```python\nfrom markdownfield.validators import VALIDATOR_NULL\n```\nThis validator does not call [bleach](https://github.com/mozilla/bleach) to sanitize the output at all.\nThis is **not safe for user input**.  It allows arbitrary (unsafe) HTML in your markdown input.\n\n\n### Creating Custom Validators\nTo create a custom validator, just create an instance of  the `markdownfield.validators.Validator`\ndataclass. An example of this is shown below.\n\n```python\nfrom markdownfield.validators import Validator\n\n# allows only bold and italic text\nVALIDATOR_COMMENTS = Validator(\n    allowed_tags=[\"b\", \"i\", \"strong\", \"em\"],\n    allowed_attrs={},\n    linkify=False\n)\n```\n\nYou can also find a standard set of markdown-safe tags and attrs in `markdownfield.validators`, and extend\nthat.\n\n```python\nfrom markdownfield.validators import Validator, MARKDOWN_TAGS, MARKDOWN_ATTRS\n\n# allows all standard markdown features,\n# but also allows the class to be set on images and links\nVALIDATOR_CLASSY = Validator(\n    allowed_tags=MARKDOWN_TAGS,\n    allowed_attrs={\n        **MARKDOWN_ATTRS,\n        'img': ['src', 'alt', 'title', 'class'],\n        'a': ['href', 'alt', 'title', 'name', 'class']\n    }\n)\n```\n\n## Migrations\n\nIf you need to migrate from TextField or CharField to the MarkdownField you need to migrate the stored `text` also in the `rendered_text` field.\nUpdate your auto-created migration file and add the method below. \nUse a method to `save()` every instance of your model once after the migrations, so the text will be copied into the `text_rendered` field correctly.\n\n```python\nfrom django.db import migrations\nimport markdownfield.models\n\n\ndef save_text_rendered(apps, schema_editor):\n    ExampleModel = apps.get_model('yourapp', 'ExampleModel')\n    for examplemodel in ExampleModel.objects.all():\n        examplemodel.save()\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('yourapp', '000X_migrate_to_markdownfield'),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name='yourapp',\n            name='text_rendered',\n            field=markdownfield.models.RenderedMarkdownField(default=''),\n            preserve_default=False,\n        ),\n        migrations.AlterField(\n            model_name='ExampleModel',\n            name='text',\n            field=markdownfield.models.MarkdownField(rendered_field='text_rendered'),\n        ),\n        migrations.RunPython(save_text_rendered),\n    ]\n```\n\n## License\n\nThis software is released under the MIT license.\n```\nCopyright (c) 2019-2021 Luke Rogers\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmptrluke%2Fdjango-markdownfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmptrluke%2Fdjango-markdownfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmptrluke%2Fdjango-markdownfield/lists"}