1.簡介
KindEditor 是一套開源的在線HTML編輯器,主要用於讓用戶在網站上獲得所見即所得編輯效果,開發人員可以用 KindEditor 把傳統的多行文本輸入框(textarea)替換爲可視化的富文本輸入框。 KindEditor 使用 JavaScript 編寫,可以無縫地與 Java、.NET、PHP、ASP 等程序集成,比較適合在 CMS、商城、論壇、博客、Wiki、電子郵件等互聯網應用上使用
2.主要特點
快速:體積小,加載速度快
開源:開放源代碼,高水平,高品質
底層:內置自定義 DOM 類庫,精確操作 DOM
擴展:基於插件的設計,所有功能都是插件,可根據需求增減功能
風格:修改編輯器風格非常容易,只需修改一個 CSS 文件
兼容:支持大部分主流瀏覽器,比如 IE、Firefox、Safari、Chrome、Opera
3.使用
3.1下載路徑: http://kindeditor.net/down.php
下載後根據需求刪除以下目錄。
asp - ASP程序
asp.net - ASP.NET程序
php - PHP程序
jsp - JSP程序
examples - 演示文件
3.2將文件夾拷貝到項目根目錄的/static/文件夾中:
3.3在kineeditor目錄下創建conifg.js配置文件
#config.js
KindEditor.ready(function(K) {
// K.create('textarea[name=content]', {
K.create('#id_content', {
width: '800px',
height: '500px',
});
});
註釋: 這裏的#id_content,或是name=content,是通過登錄admin後,右擊對應控件,選擇審查元素獲得的。
3.4在admin.py對應的管理類中添加class Media,引入js文件。
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ['title']
class Media:
js = ('/static/js/kindeditor-4.1.10/kindeditor-all-min.js',
'/static/js/kindeditor-4.1.10/lang/zh-CN.js',
'/static/js/kindeditor-4.1.10/config.js')
admin.site.register(Article,ArticleAdmin)
Blog中有文章Model,文章內容會包括各種格式的數據,比如:圖片、超鏈接、段落等。爲了達到這個目的,我們可以使用富文本編輯器。
我們有多重選擇來使用富文本編輯器,比如kindeditor、django-ckeditor、自定義ModelAdmin的媒體文件。
這樣就將kindeditor加上了富文本編輯器。
4.圖片上傳
但是如果我們上次圖片仍然會報錯,因爲我們並沒有處理文件上傳按鈕。
4.1:在config.js加入
'uploadJson':'/admin/upload/kindeditor',
這裏/admin/upload/kindeditor是python的路由。
在url.py中有配置url(r'^admin/upload/(?P<dir_name>[^/]+)$', upload_image, name='upload_image'),
dir_name是文件的存儲路徑。
4.2:upload_image是自定義的保存圖片的函數,新建upload.py 。
from django.http import HttpResponse
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
import os
import uuid
import json
import datetime as dt
@csrf_exempt
def upload_image(request, dir_name):
##################
# kindeditor圖片上傳返回數據格式說明:
# {"error": 1, "message": "出錯信息"}
# {"error": 0, "url": "圖片地址"}
##################
result = {"error": 1, "message": "上傳出錯"}
files = request.FILES.get("imgFile", None)
if files:
result = image_upload(files, dir_name)
return HttpResponse(json.dumps(result), content_type="application/json")
# 目錄創建
def upload_generation_dir(dir_name):
today = dt.datetime.today()
url_part = dir_name + '/%d/%d/' % (today.year, today.month)
dir_name = os.path.join(dir_name, str(today.year), str(today.month))
print("*********", os.path.join(settings.MEDIA_ROOT, dir_name))
if not os.path.exists(os.path.join(settings.MEDIA_ROOT, dir_name)):
os.makedirs(os.path.join(settings.MEDIA_ROOT, dir_name))
return dir_name,url_part
# 圖片上傳
def image_upload(files, dir_name):
# 允許上傳文件類型
allow_suffix = ['jpg', 'png', 'jpeg', 'gif', 'bmp']
file_suffix = files.name.split(".")[-1]
if file_suffix not in allow_suffix:
return {"error": 1, "message": "圖片格式不正確"}
relative_path_file, url_part = upload_generation_dir(dir_name)
path = os.path.join(settings.MEDIA_ROOT, relative_path_file)
print("&&&&path", path)
if not os.path.exists(path): # 如果目錄不存在創建目錄
os.makedirs(path)
file_name = str(uuid.uuid1()) + "." + file_suffix
path_file = os.path.join(path, file_name)
file_url =settings.MEDIA_URL + url_part +file_name
open(path_file, 'wb').write(files.file.read())
return {"error": 0, "url": file_url}
文件保存後,路徑爲
4.3:使用django配置/upload來顯示圖片。
from django.views.static import serve
url(r'^upload/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, }),
4.4:setting增加media的配置
MEDIA_URL = "/upload/"
MEDIA_ROOT = os.path.join(BASE_DIR, "upload")
參考文章
http://www.cnblogs.com/wupeiqi/articles/6307554.html
https://www.cnblogs.com/yangshl/p/6505308.html