您的位置:首页 > 产品设计 > UI/UE

django应用百度ueditor图片上传 解决方法

2012-07-03 09:06 896 查看
目前百度提供的ueditor只有PHP,JSP,ASP版本,我们自己来改成django版本。

首先我是把ueditor 放在/home/yeelone/django/mysite/myapp/static 里。

以下先做准备工作:

1、修改django的settings配置文件,方法请看这篇文章:

django设置静态文件路径方法

django 取消检查csrf

2、创建相应的文件夹: media 文件夹

[yeelone@lvs mysite]$ ls
myapp  django.wsgi  manage.py  media  mysite


3、修改ueditor 的editor_config.js:

/home/yeelone/django/mysite/myapp/static/ueditor


var path =  "http://"+window.location.host+"/";

window.UEDITOR_CONFIG = {

//为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL : URL

//图片上传配置区
,imageUrl:path+ "image/imageUp/"             //图片上传提交地址
,imagePath:""
imagePath 要置空,举个例子,如果你的网址是这样 http://example.com/ 设置了image/imageUp/ 做为图片上传路径,那么访问的会是 http://example.com/[imagePath]指定的地址/image/imageUp 所以才必须置空,使之成为: http://example.com/image/imageUp


4、接下来写urls.py

(r'^image/imageUp/$',imageUp),


5、写views.py

def imageUp(request):
#    print request.FILES
if request.method == 'POST':
b = save_file(request.FILES['upfile'])
return  HttpResponse(b)

def save_file(file,path=''):
filename   = str(time.time()) + str(random.random()) + file._get_name()
print filename
fd = open('%s/%s' % (settings.MEDIA_ROOT,str(path)+str(filename)),'wb')
for chunk  in file.chunks():
fd.write(chunk)
fd.close()
a  ="{'url':'/media/"+filename+"','title':'"+filename+"','state':'SUCCESS'}"

return a

在上传图片成功时要在后台返回:

{
"url"
:
"图片地址"
,
"title"
:
"图片描述"
,
"state"
:
"上传状态"
}


这样的 json数据。

以上已经完成图片上传。看效果 :







本文出自 “YEELONⒼ ” 博客,请务必保留此出处http://yeelone.blog.51cto.com/1476571/916682
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐