Django富文本
本文 使用 django-ckeditor (个人UI界面比较好看)
安装插件
1
pip install django-ckeditor -i https://mirrors.aliyun.com/pypi/simple
在Django项目中注册插件
1、修改settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'App',
# 注册ckeditor
'ckeditor',
# 'ckeditor_uploader',
]2、在 models.py 创建模型
1
2
3
4
5from ckeditor.fields import RichTextField
from django.db import models
class Blog(models.Model):
text = RichTextField()3、进行数据迁移
1
2python manage.py makemigrations
python manage.py migrate
在HTML使用django-ckeditor
1
2
3
4
5
6
7
8
9
10
11
12{# 在head添加 #}
{% load static %}
<script type="text/javascript" src="{% static "ckeditor/ckeditor/ckeditor.js" %}"></script>
{# 富文本使用 #}
<form action="#" method="post">
{% csrf_token %}
<textarea name="editor1"></textarea>
<script>
CKEDITOR.replace( 'editor1' , {height: 600, width: 1000, toolbar: 'Custom'});
</script>
</form>