目录

使用方法

直接给输入框绑定事件即可,注意引入js方式有点不一样,多加编码方式

<script charset="utf-8" src="/editor/kindeditor.js"></script>
<script charset="utf-8" src="/editor/lang/zh-CN.js"></script> KindEditor.ready(function(K) {
window.editor = K.create('#editor_id');
}); //添加富文本编辑菜单栏 K.create('要绑定事件的文本输入框id',{初识化数据放在这里})

K.create有两个参数,参数以要绑定标签的id值,参数2,初始化参数

    KindEditor.ready(function (K) {
window.editor = K.create('#editor_id',
{
width: '100%', //宽度支持%和px样式
items: [
'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
'anchor'
]
})
;
});

官方网址

富文本编辑器官方网站 http://kindeditor.net/docs/option.html#items

图片上传下载实例

视图处理

def up_img(request,):
img_obj = request.FILES.get("imgFile",None)
# 图片的名字默认是imgFile path=os.path.join(settings.MEDIA_ROOT,"article_img",img_obj.name)# 拿到文件上传的路径,保存到medio文件中,方便访问
# 默认名字是imgFile
with open(path,"wb") as f:
for i in img_obj:
f.write(i)
print(path)
data={
"error":0, # 给编辑器返回上传结果
"url":"/media/article_img/"+img_obj.name # 返回图片路径,编辑器访问浏览器取数据
}
return HttpResponse(json.dumps(data))

注意回复的一定是json格式字符串

js事件


KindEditor.ready(function (K) {
window.editor = K.create('#editor_id',
{
width: '100%',
uploadJson:"/up_img/",
extraFileUploadParams:{ //相当于ajax的data
csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val()
}
})
;
}); //添加富文本编辑菜单栏

filePostName指定上传文件form名称。

数据类型: String

默认值: imgFile

菜单栏功能筛选

 items: [
'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
'anchor'
],

bs4

导入

pip3 install Beautifulsoup4

提取标签内的文本内容

对某一个标签内容进行提取

soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')

s = soup.b.string

print(s)        # Extremely bold

print(type(s))  # <class 'bs4.element.NavigableString'>

对全部内容进行提取

from bs4 import BeautifulSoup
def editor_article(request,):
if request.method == "POST":
title=request.POST.get("title",None)
content=request.POST.get("content",None)
bs=BeautifulSoup(content,"html.parser")
print(22)
desc=bs.text[0:150]+"..." print(desc)
article_obj=models.Article.objects.create(title=title,desc=desc,user=request.user)
models.ArticleDetail.objects.create(article=article_obj,content=content)
return redirect("/home/")
print(request.user)
return render(request,"editor_article.html",{"request":request})

注意,text不是方法,而是属性,不需要加(),可以对结果进行提取

在编辑框中必须点击,HTML编写文章,后端才能够截取内容,系统默认,输入的内容都是字符串形式

如果不对bs文件进行截取数据,就有可能在文章简介布局时,出现内容错乱问题,主要是因为上传的文件是HTML格式时,在布局HTML中用

artitle.desc|safa

就会出现布局错乱,主要是因为截取的内容不完整,可能截取的标签不闭合,与自己写的标签形成闭合,这样就会出现布局错乱

富文编辑器和bs4简单实用的更多相关文章

  1. python 全栈开发,Day83(博客系统子评论,后台管理,富文本编辑器kindeditor,bs4模块)

    一.子评论 必须点击回复,才是子评论!否则是根评论点击回复之后,定位到输入框,同时加入@评论者的用户名 定位输入框 focus focus:获取对象焦点触发事件 先做样式.点击回复之后,定位到输入框, ...

  2. wpf 富文本编辑器richtextbox的简单用法

    最近弄得一个小软件,需要用到富文本编辑器,richtextbox,一开始以为是和文本框一样的用法,但是实践起来碰壁之后才知道并不简单. richtextbox 类似于Word,是一个可编辑的控件.结构 ...

  3. 富文本编辑器Quill(一)简单介绍

    Quill是一个很流行的富文本编辑器,github上star大约21k: github:https://github.com/quilljs/quill/ 官网: https://quilljs.co ...

  4. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  5. react-quill 富文本编辑器

    适合react的一款轻量级富文本编辑器 1.http://blog.csdn.net/xiaoxiao23333/article/details/62055128 (推荐一款Markdown富文本编辑 ...

  6. 富文本编辑器--引入demo和简单使用

    wangEditor —— 轻量级 web 富文本编辑器,配置方便,使用简单.支持 IE10+ 浏览器. 官网:www.wangEditor.com 文档:www.kancloud.cn/wangfu ...

  7. 基于ABP做一个简单的系统——实战篇:4.基于富文本编辑器,Razor模板引擎生成内容并导出Word 填坑记录

    起因 需求是这样的,有一种协议需要生成,协议的模板是可配置的,在生成过程中,模板中的内容可以根据约定的标记进行替换(就像mvc的razor模板一样).生成后的内容还需要导出成word或pdf. 常见的 ...

  8. .net下将富文本编辑器文本原样读入word文档

    关键词:富文本编辑器  生成word  样式 为了解决标题中提出的问题,首选需要了解,在.net环境下读取数据库中的内容动态生成word至少有2种方式,[方式一]一种方式是在项目中添加引用,例如在“添 ...

  9. 富文本编辑器UEditor自定义工具栏(三、自定义工具栏功能按钮图标及工具栏样式简单修改)

    导读 富文本编辑器UEditor提供丰富了定制配置项,如果想设置个性化的工具栏按钮图标有无办法呢?答案是肯定的!前两篇博文简要介绍了通过将原工具栏隐藏,在自定义的外部按钮上,调用UEditor各命令实 ...

随机推荐

  1. cf--703--A-- Mishka and Game

    题目链接:http://codeforces.com/problemset/problem/703/A Mishka is a little polar bear. As known, little ...

  2. grep : app :Is a directory

    今天在查日志的时候用grep命令,遇到这样的一个问题,grep : app :Is a directory 用的grep命令是这样的:grep -10 '2019-08-14 21:22:39.252 ...

  3. 【Redis】基础学习概览【汇总】

    一.概述 1.1 简介 1.2 Redis单线程好处 1.3 单线程弊端 1.4 Redis应用场景 二.安装.开启以及关闭 三.Redis基本数据类型 四.SpringBoot整合Redis 五.R ...

  4. 集成学习方法Boosting和Bagging

    集成学习是通过构架并结合多个学习器来处理学习任务的一种思想, 目前主要分为两大类:Boosting和Bagging. 对于任意一种集成方法, 我们都希望学习出来的基分类器具有较高的准确性和多样性, 基 ...

  5. springboot应用监控和管理

    spring boot应用监控和管理 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控 ...

  6. LaTeX 自动避免重复内容

    在编辑自动化文档时,很容易出现在文档多处提及相同内容的情况.例如,描述某具体设备的图片,在多个工艺中都会用到,而又无法确定工艺出现顺序,或者对于不同企业,工艺不尽相同.这时我们可能会希望,latex帮 ...

  7. 去掉Myeclipse对JS等文件的验证

    在用Myeclipse导入新工程或在写代码时,最郁闷的就是它对js,jsp,html的验证太严格了,有时会呈现一个红叉或者一个黄色的感慨号,一运行就报Cannot return from outsid ...

  8. PHP 错误:Warning: Cannot modify header information - headers already sent by ...

    PHP初学者容易遇到的错误:Warning: Cannot modify header information - headers already sent by ...: 通常是由不正确使用 hea ...

  9. map转java对象

    pom依赖: <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons ...

  10. Android mmap 文件映射到内存介绍

    本文链接: Android mmap 文件映射到内存介绍 Android开发中,我们可能需要记录一些文件.例如记录log文件.如果使用流来写文件,频繁操作文件io可能会引起性能问题. 为了降低写文件的 ...