使用Django做项目时,有时会有使用到富文本编辑器的需求。例如论坛发帖及回帖;发表博客,回复博客等。
安装django-tinymce库
pip install django-tinymce
settings.py添加配置
INSTALLED_APPS = [
...,
# 添加应用
'tinymce',
]
# 富文本剪辑器配置
TINYMCE_DEFAULT_CONFIG = {
'theme': 'advanced',
'width': 600,
'height': 400
}
视图函数
def editor(request):
if request.method == 'POST':
content = request.POST.get('content')
return HttpResponse(content)
return render(request, 'app01/editor.html')
前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="/static/tiny_mce/tiny_mce.js"></script>
<script>
tinyMCE.init({
'mode':'textareas',
'width':800,
'height':600,
})
</script>
<title>Rich text editor</title>
</head>
<body>
<form action="{% url 'App01:editor' %}" method="post">
{% csrf_token %}
<textarea name="content" id="content" cols="30" rows="10"></textarea> <br>
<input type="submit">
</form>
</body>
</html>