zoukankan      html  css  js  c++  java
  • Django使用富文本编辑器

    1.下载kindeditor
      网址:http://kindeditor.net/demo.php
    2.解压到项目中
      地址:staticjskindeditor-4.1.10
    3.删除没用的文件
      例如:example,php,asp等
    4.在需要使用富文本编辑器的model中定义meta类:

    class Media:
            js = (
                '/static/js/kindeditor-4.1.10/kindeditor-min.js',
                '/static/js/kindeditor-4.1.10/lang/zh_CN.js',
                '/static/js/kindeditor-4.1.10/config.js',
            )

    5.在kindeditor-4.1.10目录中定义config.js文件:

    KindEditor.ready(function(K) {
                    K.create('textarea[name=需要使用富文本的字段名]',{
                        '800px',
                        height:'200px',
                uploadJson: '/util/upload/kindeditor',
    }); });

     6.在urls.py中定义文件上传的处理器:

    from util.upload import upload_image
    
    url(r'^util/upload/(?P<dir_name>[^/]+)$', upload_image, name='upload_image'),

    7.在settings.py中定义文件上传的目录如下:

    MEDIA_URL = '/uploads/'
    
    MEDIA_ROOT = os.path.join(BASE_DIR,  'uploads')

    8.处理upload的py文件:

    # -*- coding: utf-8 -*-
    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()
        dir_name = dir_name + '/%d/%d/' %(today.year,today.month)
        if not os.path.exists(settings.MEDIA_ROOT + dir_name):
            os.makedirs(settings.MEDIA_ROOT + dir_name)
        return dir_name
    
    # 图片上传
    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 = upload_generation_dir(dir_name)
        path=os.path.join(settings.MEDIA_ROOT, relative_path_file)
        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 + relative_path_file + file_name
        open(path_file, 'wb').write(files.file.read()) # 保存图片
        return {"error": 0, "url": file_url}
  • 相关阅读:
    执行chmod -R 777 / 补救
    kill详解
    find详解
    htop详解
    C#正则表达式经典分类整理集合手册
    C# 正则表达式大全
    各种新主流.net混淆加密软件对比
    string format double
    System.Timers.Timer
    System.Threading.Timer
  • 原文地址:https://www.cnblogs.com/413xiaol/p/6852988.html
Copyright © 2011-2022 走看看