Skip to main content

A Mdeditor plugin for Django5.

Project description

django5-mdeditor

Django5-mdeditor is an improved version based on Django-mdeditor. As Django-mdeditor has not been updated for many years, this tool was created.

The usage of this plugin is no different from that of Django mdeditor, except that during installation, pip install Django 5-mdeditor needs to be used, as detailed in the following text.

Django-mdeditor is Markdown Editor plugin application for django base on Editor.md.

Django-mdeditor was inspired by great django-ckeditor.

Note:

  • For Markdown page rendering issues, backend rendering is recommended. Because Editor.md has not been updated for a long time, some bugs and compatibility issues need to be debugged. Of course, front-end students can choose.

  • Regarding the Jquery conflict, it cannot be deleted because it is required by the admin backend. It is recommended to separate the editing page on a single page or a full screen directly, using its own static file to distinguish it from other pages.

Features

  • Almost Editor.md features

    • Support Standard Markdown / CommonMark and GFM (GitHub Flavored Markdown);

    • Full-featured: Real-time Preview, Image (cross-domain) upload, Preformatted text/Code blocks/Tables insert, Search replace, Themes, Multi-languages;

    • Markdown Extras : Support ToC (Table of Contents), Emoji;

    • Support TeX (LaTeX expressions, Based on KaTeX), Flowchart and Sequence Diagram of Markdown extended syntax;

  • Can constom Editor.md toolbar

  • The MDTextField field is provided for the model and can be displayed directly in the django admin.

  • The MDTextFormField is provided for the Form and ModelForm.

  • The MDEditorWidget is provided for the Admin custom widget.

Quick start

  • Installation.
    pipenv install django5-mdeditor

    # or

    pip install django5-mdeditor
  • Add mdeditor to your INSTALLED_APPS setting like this:
    INSTALLED_APPS = [

        ...

        'mdeditor',

    ]
  • add frame settings for django3.0+ like this:
X_FRAME_OPTIONS = 'SAMEORIGIN' 
  • Add 'media' url to your settings like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')

MEDIA_URL = '/media/'

Make folder uploads/editor in you project for media files.

  • Add url to your urls like this:
from django.conf.urls import url, include

from django.conf.urls.static import static

from django.conf import settings

...



urlpatterns = [

    ...

    url(r'mdeditor/', include('mdeditor.urls'))

]



if settings.DEBUG:

    # static files (images, css, javascript, etc.)

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Write your models like this:
from django.db import models

from mdeditor.fields import MDTextField



class ExampleModel(models.Model):

    name = models.CharField(max_length=10)

    content = MDTextField()
  • Register your model in admin.py

  • Run python manage.py makemigrations and python manage.py migrate to create your models.

  • Login Admin ,you can see a markdown editor text field like this:

Usage

Edit fields in the model using Markdown

Using Markdown to edit the fields in the model, we simply replace the TextField of the model with MDTextField.

from django.db import models

from mdeditor.fields import MDTextField



class ExampleModel (models.Model):

    name = models.CharField (max_length = 10)

    content = MDTextField ()

Admin in the background, will automatically display markdown edit rich text.

Used in front-end template, you can use like this:

{% load staticfiles %}

<! DOCTYPE html>

<html lang = "en">

    <head>

        <meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />



    </ head>

    <body>

        <form method = "post" action = "./">

            {% csrf_token %}

            {{ form.media }}

            {{ form.as_p }}

            <p> <input type = "submit" value = "post"> </ p>

        </ form>

    </ body>

</ html>

Edit fields in the Form using markdown

Use markdown to edit fields in the Form, use MDTextFormField instead of forms.CharField, as follows:

from mdeditor.fields import MDTextFormField



class MDEditorForm (forms.Form):

    name = forms.CharField ()

    content = MDTextFormField ()

ModelForm can automatically convert the corresponding model field to the form field, which can be used normally:

class MDEditorModleForm (forms.ModelForm):



    class Meta:

        model = ExampleModel

        fields = '__all__'

Use the markdown widget in admin

Use the markdown widget in admin like as :

from django.contrib import admin

from django.db import models



# Register your models here.

from. import models as demo_models

from mdeditor.widgets import MDEditorWidget





class ExampleModelAdmin (admin.ModelAdmin):

    formfield_overrides = {

        models.TextField: {'widget': MDEditorWidget}

    }





admin.site.register (demo_models.ExampleModel, ExampleModelAdmin)

Customize the toolbar

Add the following configuration to settings:

MDEDITOR_CONFIGS = {

    'default':{

        'width': '90% ',  # Custom edit box width

        'height': 500,  # Custom edit box height

        'toolbar': ["undo", "redo", "|",

                    "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",

                    "h1", "h2", "h3", "h5", "h6", "|",

                    "list-ul", "list-ol", "hr", "|",

                    "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime",

                    "emoji", "html-entities", "pagebreak", "goto-line", "|",

                    "help", "info",

                    "||", "preview", "watch", "fullscreen"],  # custom edit box toolbar 

        'upload_image_formats': ["jpg", "jpeg", "gif", "png", "bmp", "webp"],  # image upload format type

        'image_folder': 'editor',  # image save the folder name

        'theme': 'default',  # edit box theme, dark / default

        'preview_theme': 'default',  # Preview area theme, dark / default

        'editor_theme': 'default',  # edit area theme, pastel-on-dark / default

        'toolbar_autofixed': True,  # Whether the toolbar capitals

        'search_replace': True,  # Whether to open the search for replacement

        'emoji': True,  # whether to open the expression function

        'tex': True,  # whether to open the tex chart function

        'flow_chart': True,  # whether to open the flow chart function

        'sequence': True, # Whether to open the sequence diagram function

        'watch': True,  # Live preview

        'lineWrapping': False,  # lineWrapping

        'lineNumbers': False,  # lineNumbers

        'language': 'zh'  # zh / en / es 

    }

    

}

Feedback

Welcome to use and feedback!

You can create a issue or join in QQ Group.

Reference

Download files

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

Source Distribution

django5_mdeditor-1.0.2.tar.gz (2.7 MB view details)

Uploaded Source

Built Distribution

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

django5_mdeditor-1.0.2-py3-none-any.whl (1.8 MB view details)

Uploaded Python 3

File details

Details for the file django5_mdeditor-1.0.2.tar.gz.

File metadata

  • Download URL: django5_mdeditor-1.0.2.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for django5_mdeditor-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7cf4aac80669aafb7c1a028af76abd017ba0d247da5bbebac95acc1adc122465
MD5 cfe126912c0c251c7e9c50cca1f58c9a
BLAKE2b-256 efea64d9d98a0dd31469d829a8d0f34ee15fdb3e33d52cc8e064490bf1187deb

See more details on using hashes here.

File details

Details for the file django5_mdeditor-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django5_mdeditor-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c62165abd2c9472a04ff26f8c5c6e12e57bd8f010e527c9a7e7a8cccd624c4c9
MD5 2dc2ba8eac6435b6be38bcf615858a77
BLAKE2b-256 b8120e0a8604a7183e9b4d26c4aa6d9dd76169f9f6bd1a6843fb7501be115edd

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