Django换用Markdown编辑器
参考:
安装所需包
pip install django-mdeditor
报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 167: illegal multibyte sequence
最新版本中有冲突
换用pip install django-mdeditor==0.1.16
pip install markdown
Django配置
配置settings.py
-
INSTALLED_APPS
里添加mdeditor
INSTALLED_APPS = [
...
'mdeditor',
]
- 添加媒体文件路径配置
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/media/'
配置urls.py
urlpatterns = [
...
path('mdeditor'), include('mdeditor.urls'),
]
修改models
from mdeditor import MDTextField
...
content = MDTextField()
修改views
blog.content = markdown.markdown(blog.content)
执行数据库迁移
python manage.py makemigrations
python manage.py migrate