DjangoUeditor-Python3

安装

github下载DjangoUeditor

直接使用git克隆, 里面有一个setup.py文件

运行安装: python setup.py install

使用

  1. url.py 中添加

    url(r'^ueditor/', include('DjangoUeditor.urls')),
    
  2. settings.py 中安装app

    'DjangoUeditor'			# 添加这样一个App
    
    # 注意配置settings中的MEDIA_ROOT, NEDIA_URL, 上传文件的存储依赖于它
    
  3. 模板中使用

    注意js路径报错,如果提示没有哪个js文件,去网上下载放在所提示的位置.

    <script type="text/javascript" charset="utf-8">
            window.UEDITOR_HOME_URL = "/static/js/ueditor/";
        </script>
    <script type="text/javascript" src="/static/js/lib/jquery2.2.4/jquery-2.2.4.js"></script>
    <script type="text/javascript" src="/static/js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" src="/static/js/ueditor/ueditor.all.min.js"></script>
    
    <--! 这就是编辑框  -->
    <textarea id="content" name="content"  style="575px;line-height:18px;"></textarea>
    
    
    <script>
         var ue = UE.getEditor('content', {
            'initialFrameWidth': 800,
            'initialFrameHeight': 300,
            'serverUrl': '/ueditor/controller/?imagePathFormat=&filePathFormat=',
        });
    </script>
    </html>
    
  4. 迁移静态文件

    python manage.py collectstatic
    # 这条命令将所有用到的js文件等等都会加载过来,当然也有没用的,可以删掉
    
  5. 修改DjangoUeditor

    此时不出意外的话你已经可以上传文件了, 但是涂鸦功能不能使用

    • 打开DjangoUeditor源码,路径:python安装包的位置/lib/python3.x/site-packages/DjangoUeditor/views.py

    • 在views.py的最后,修改涂鸦上传的函数

      # 涂鸦功能上传处理
      @csrf_exempt
      def save_scrawl_file(request, filename):
          import base64
          try:
              content = request.POST.get(USettings.UEditorUploadSettings.get("scrawlFieldName", "upfile"))
              f = open(filename, 'wb')
              # python3 中好像已经弃用decodestring这个函数
              # f.write(base64.decodestring(content))
              # 必须以二进制的形式写入
              f.write(base64.decodebytes(bytes(content, 'utf-8')))
              f.close()
              state = "SUCCESS"
          except Exception as E:
              # 此处异常捕获需要改为python3的用法
              state = "写入图片文件错误: {}".format(E)
          return state
      
  6. 这些问题都是我踩过的坑,自己也网上查资料解决. 也感谢github上面的大佬.

原文地址:https://www.cnblogs.com/sajinchang/p/10642992.html