简体   繁体   English

使用Django Ckeditor上传图像-获取服务器错误(500)

[英]Uploading images use Django Ckeditor --getting server error(500)

Uploading images use Django Ckeditor --getting server error(500) 使用Django Ckeditor上传图像-获取服务器错误(500)

I have been struggling for the problem for two days. 我为这个问题苦苦挣扎了两天。 Unfortunately, i still have no idea to solve it due to my poor knowledge. 不幸的是,由于我的知识不足,我仍然不知道要解决它。 So i have to come here asking for help. 所以我必须来这里寻求帮助。 Very appreciate ! 非常感谢!

在此处输入图片说明

I want to write a website as my blog and use Django to implement it. 我想写一个网站作为我的博客,并使用Django来实现它。 To develop this website, i have to use rich text editor, so i use CKeditor on admin panel. 要开发此网站,我必须使用RTF编辑器,因此我在管理面板上使用CKeditor。 Here is the link of Ckeditor source code on github. 这是github上Ckeditor源代码的链接。 https://github.com/django-ckeditor/django-ckeditor https://github.com/django-ckeditor/django-ckeditor

To upload images using ckeditor widget , i edited this file ../static/ckeditor/ckeditor/plugins/image/di alogs/image.js so it can display the images upload button. 要使用ckeditor小部件上载图像,我编辑了此文件../static/ckeditor/ckeditor/plugins/image/di alogs/image.js以便它可以显示图像上载按钮。

id:"Upload",hidden:!0

I also added upload url in config.js . 我还在config.js添加了上传网址。 After that, i set routing in the urls.py and added a view function in views.py. 之后,我在urls.py设置路由,并在views.py中添加了一个视图功能。 Everything was ok on my computer. 我的电脑上一切正常。 however, after i deployed it to website server i got server error(500) during uploading a image by ckeditor . 但是,在将它部署到网站服务器后,我通过ckeditor上传图像时出现服务器错误(500)。 Ckeditor widget can not return the urls but i can find the images on server which i uploaded by ckeditor. Ckeditor小部件无法返回URL,但是我可以在我由ckeditor上传的服务器上找到图像。

$:~/sites/www/source$ ls ../media/images/
20161219045646_7.jpeg           20161219053949_0094.jpg      
$:~/sites/www/source$

config.js (the location static/ckeditor/ckeditor/ ) config.js (位置static/ckeditor/ckeditor/

/**
 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben.   All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

CKEDITOR.editorConfig = function( config ) {
        config.filebrowserImageUploadUrl="/articleuploadimg/";
};

urls.py urls.py

from django.conf.urls import url, include
from django.contrib import admin
from article import views as article_views
urlpatterns = [
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^articleuploadimg/', article_views.article_upload_image),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The file tree 文件树

|___sites
        |___www.mysite.com
                |___database
                |    |___db.sqlite3
                |   
                |___source
                |    |___manage.py
                |    |___article
                |    |      |___views.py
                |    |      |___...
                |    |___...
                |   
                |___static
                |    |___ckeditor
                |    |___css
                |    |___js
                |     
                |___virtualenv
                |___media
                     |___images

views.py views.py

from django.shortcuts import render
from article.models import Article
from django.views.decorators.csrf import csrf_protect
import time

@csrf_protect
def article_upload_image(request):
    if request.method == 'POST':
        callback = request.GET.get('CKEditorFuncNum')
        try:
            path = "../../media/images/"+time.strftime("%Y%m%d_%H%M%S", time.localtime())
            f = request.FILES["upload"]
            file_name = path + "_" + f.name
            des_origin_f = open(file_name, "wb+")
            for chunk in f:
                des_origin_f.write(chunk)
            des_origin_f.close()
        except Exception as e:
            print (e)
        res = r"<script>window.parent.CKEDITOR.tools.callFunction("+callback+",'"+file_name+"','');</script>"
        return HttpResponse(res)
    else:
        raise Http404()

settings.py settings.py

# Application definition    
INSTALLED_APPS = [
    'ckeditor',
    'ckeditor_uploader',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'article',
]
...
...

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../static'))

# Media files (upload path)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../media/'))
CKEDITOR_UPLOAD_PATH = ""
CKEDITOR_JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'

I added something in /etc/nginx/sites-available/www.mysite.com 我在/etc/nginx/sites-available/www.mysite.com添加了一些/etc/nginx/sites-available/www.mysite.com

location /media {
    alias /home/XXX/sites/www.mysite.com/media;
}

as well as changed the path 以及改变了道路

path = "../media/images/"

Finally, it works ! 最后,它起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM