Skip to main content

A Django model field that integrates Python-Markdown for handling Markdown content

Project description

django-md-field

A Django model field that integrates Python-Markdown for handling Markdown content.

Requirements

  • Python: >=3.10, <4
  • Django: >=4.2, <6.0

Installation

pip install django-md-field

Usage

Basic Usage

from django.db.models import Model
from markdown_field import MarkdownField

class Post(Model):
    body = MarkdownField("body")

Accessing Content

# Create a post with Markdown content
>>> post = Post.objects.create(body="## Heading\n\nThis is a paragraph.")

# Access the raw Markdown text
>>> post.body.text
'## Heading\n\nThis is a paragraph.'

# Access the rendered HTML
>>> post.body.html
'<h2>Heading</h2>\n<p>This is a paragraph.</p>'

# Access table of contents (if TOC extension is enabled)
>>> post.body.toc
'<li><a href="#heading">Heading</a></li>'

Configuring Markdown Extensions

You can configure Markdown extensions in two ways:

  • In your Django settings (settings.py):
from markdown.extensions.toc import TocExtension
from django.utils.text import slugify

MARKDOWN_FIELD = {
    "extensions": [
        TocExtension(slugify=slugify, toc_depth=3),
        "pymdownx.highlight",
        "pymdownx.arithmatex",
    ],
    "extension_configs": {
        "pymdownx.highlight": {
            "linenums_style": "pymdownx-inline",
        },
        "pymdownx.arithmatex": {
            "generic": True,
        },
    },
}
  • Directly in the model field, this will override the MARKDOWN_FIELD setting in settings.py:
from markdown.extensions.toc import TocExtension
from django.utils.text import slugify

body = MarkdownField(
    "body",
    extensions=[
        TocExtension(slugify=slugify, toc_depth=3),
        "pymdownx.highlight",
        "pymdownx.arithmatex",
    ],
    extension_configs={
        "pymdownx.highlight": {
            "linenums_style": "pymdownx-inline",
        },
        "pymdownx.arithmatex": {
            "generic": True,
        },
    },
)

For more information about available extensions, see:

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_md_field-0.1.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_md_field-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_md_field-0.1.0.tar.gz.

File metadata

  • Download URL: django_md_field-0.1.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for django_md_field-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87dff61316dfd8825b93793b91de2b9fe7e2d7f3b52478ed751565931195c2ff
MD5 d1e816e58e1b92bac02ec2810488badf
BLAKE2b-256 0f65bf295822108b99b2cd7addaa6a4c0cea8faeec51dfd328bab2aef08012c5

See more details on using hashes here.

File details

Details for the file django_md_field-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_md_field-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 041cd2e55551ab6aa8085ffd1819a9036de7f832f29e66ef3a0eb649c6c7bfc5
MD5 6f1a43430c431463732c42519977773b
BLAKE2b-256 037c36c5d78a382b4b1a80b6596c7020b0eafeb26b43514cece08deb904d4e2b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page