上传图片使用Django Ckeditor --getting server error(500)
Uploading images use Django Ckeditor --getting server error(500)
上传图片使用Django Ckeditor --getting server error(500)
这两天一直在为这个问题苦苦挣扎。不幸的是,由于我的知识不足,我仍然不知道如何解决它。所以我必须来这里寻求帮助。非常感谢!
我想写一个网站作为我的博客,用Django实现。
为了开发这个网站,我必须使用富文本编辑器,所以我在管理面板上使用了 CKeditor。这是 github 上的 Ckeditor 源代码 link。 https://github.com/django-ckeditor/django-ckeditor
为了使用 ckeditor 小部件上传图片,我编辑了这个文件../static/ckeditor/ckeditor/plugins/image/di
alogs/image.js
,以便它可以显示图片上传按钮。
id:"Upload",hidden:!0
我还在config.js
中添加了上传url。之后,我在urls.py
中设置路由并在views.py中添加视图功能。我的电脑上一切正常。然而,在我将它部署到网站服务器后,我在通过 ckeditor 上传图像时遇到了服务器错误(500)。 Ckeditor 小部件无法 return url,但我可以在服务器上找到由 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'
我在/etc/nginx/sites-available/www.mysite.com
中添加了一些内容
location /media {
alias /home/XXX/sites/www.mysite.com/media;
}
以及更改路径
path = "../media/images/"
终于成功了!
上传图片使用Django Ckeditor --getting server error(500)
这两天一直在为这个问题苦苦挣扎。不幸的是,由于我的知识不足,我仍然不知道如何解决它。所以我必须来这里寻求帮助。非常感谢!
我想写一个网站作为我的博客,用Django实现。 为了开发这个网站,我必须使用富文本编辑器,所以我在管理面板上使用了 CKeditor。这是 github 上的 Ckeditor 源代码 link。 https://github.com/django-ckeditor/django-ckeditor
为了使用 ckeditor 小部件上传图片,我编辑了这个文件../static/ckeditor/ckeditor/plugins/image/di
alogs/image.js
,以便它可以显示图片上传按钮。
id:"Upload",hidden:!0
我还在config.js
中添加了上传url。之后,我在urls.py
中设置路由并在views.py中添加视图功能。我的电脑上一切正常。然而,在我将它部署到网站服务器后,我在通过 ckeditor 上传图像时遇到了服务器错误(500)。 Ckeditor 小部件无法 return url,但我可以在服务器上找到由 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'
我在/etc/nginx/sites-available/www.mysite.com
location /media {
alias /home/XXX/sites/www.mysite.com/media;
}
以及更改路径
path = "../media/images/"
终于成功了!