基于django快速开发一个网站(三)
* 给django集成富文本编辑器
1. 下载富文本插件到系统
pip install django-mdeditor
2. 添加mdeditor到你的应用中
INSTALLED_APPS = [ ... 'mdeditor', ]
3. 配置media到settings.py中
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads') MEDIA_URL = '/media/'
4. 配置路由
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)
5. 配置models层
from django.db import models from mdeditor.fields import MDTextField class ExampleModel(models.Model): name = models.CharField(max_length=10) content = MDTextField()
6. 运行迁移命令、使models层的数据生效
python3 manage.py makemigrations
python3 manage.py migrate