当前位置: 首页 > 工具软件 > mdEditor > 使用案例 >

Django——集成富文本编辑器mdeditor

严宇
2023-12-01

pip安装mdeditor

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple   django-mdeditor
  • 用清华源安装会快很多

settings.py配置mdeditor

  • 安装app
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mdeditor'
]
  • 设置媒体文件路径
MEDIA_ROOT = os.path.join(BASE_DIR, '[yourapp]/../uploads')
MEDIA_URL = '/[yourapp]/media/'

文件路径如下:

[yourapp]
	__init__.py
	settings.py
	urls.py
	wsgi.py
uploads
	editor

在urls.py中配置media文件上传路由

from django.views.static import serve
from [yourapp].settings import MEDIA_ROOT

urlpatterns = [
    path('media/<path:path>', serve, {'document_root':MEDIA_ROOT}),
]

编写models.py文件

from mdeditor.fields import MDTextField

# 文档列表
class DocumentList(models.Model):
    # 文档ID
    id = models.AutoField(primary_key=True, verbose_name='文档ID')
    # 名称
    title = models.CharField(max_length=50, verbose_name='名称')
    # 发布时间
    time = models.DateTimeField(auto_now=True, verbose_name='发布时间')
    # 文档内容
    content = MDTextField(null=True)
 类似资料: