2016-12-19 112 views
0

上傳圖片使用Django CKEDITOR --getting服務器錯誤(500)上傳圖片使用Django CKEDITOR --getting服務器錯誤(500)

我一直在掙扎的問題了兩天。不幸的是,由於我的知識水平不高,我仍然無法解決這個問題。所以我必須來這裏尋求幫助。非常感激!

enter image description here

我想寫一個網站,我的博客和使用Django來實現它。 爲了開發這個網站,我必須使用富文本編輯器,所以我在管理面板上使用CKeditor。以下是github上Ckeditor源代碼的鏈接。 https://github.com/django-ckeditor/django-ckeditor

要使用ckeditor小部件上傳圖像,我編輯此文件../static/ckeditor/ckeditor/plugins/image/di alogs/image.js,以便它可以顯示圖像上傳按鈕。

id:"Upload",hidden:!0 

我還在config.js上加了上傳網址。之後,我在urls.py中設置路由並在views.py中添加了一個視圖函數。我的電腦上一切正常。但是,在我部署到網站服務器後,我在ckeditor上傳圖像時出現服務器錯誤(500)。 Ckeditor小部件無法返回網址,但我可以在服務器上找到我通過ckeditor上傳的圖像。

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

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

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) 

文件樹

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

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

# 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' 

回答

0

/etc/nginx/sites-available/www.mysite.com

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

補上一以及改變路徑

path = "../media/images/" 

最後,它的工作原理!