django-blog(七)上传图片
1、在settings.py中配置MEDIA_URL和MEDIA_ROOT
[root@localhost blogproject]#
[root@localhost blogproject]# vim blogproject/settings.py
MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
2、urls.py中配置路由
django1.9及以下
url(r"^uploads/(?P<path>.*)$", \
"django.views.static.serve", \
{"document_root": settings.MEDIA_ROOT,}),
djaggo1.10及以上
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',index,name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
3、在model中设置图片的上传位置和路径
[root@localhost kindeditor-4.1.10]# cd /root/blogenv/blog_env/blogproject/static/js/kindeditor-4.1.10
[root@localhost kindeditor-4.1.10]# vim config.js
KindEditor.ready(function(K) {
window.editor = K.create('#id_content',
{
width : '900px',
height : '300px',
uploadJson: '/admin/upload/kindeditor',
}
);});
urls配置:
from django.conf.urls import url
from django.contrib import admin
from blog.views import *
from django.conf import settings
from blog.upload import upload_image
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',index,name='index'),
url(r'^admin/upload/(?P<dir_name>[^/]+)$', upload_image, name='upload_image'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
代码
[root@localhost blogproject]# vim blog/upload.py
# -*- coding: utf-8 -*- from django.http import HttpResponse from django.conf import settings from django.views.decorators.csrf import csrf_exempt import os import uuid import json import datetime as dt @csrf_exempt def upload_image(request, dir_name): ################## # kindeditor图片上传返回数据格式说明: # {"error": 1, "message": "出错信息"} # {"error": 0, "url": "图片地址"} ################## result = {"error": 1, "message": "上传出错"} files = request.FILES.get("imgFile", None) if files: result =image_upload(files, dir_name) return HttpResponse(json.dumps(result), content_type="application/json") #目录创建 def upload_generation_dir(dir_name): today = dt.datetime.today() dir_name = dir_name + '/%d/%d/' %(today.year,today.month) if not os.path.exists(settings.MEDIA_ROOT + dir_name): os.makedirs(settings.MEDIA_ROOT + dir_name) return dir_name # 图片上传 def image_upload(files, dir_name): #允许上传文件类型 allow_suffix =['jpg', 'png', 'jpeg', 'gif', 'bmp'] file_suffix = files.name.split(".")[-1] if file_suffix not in allow_suffix: return {"error": 1, "message": "图片格式不正确"} relative_path_file = upload_generation_dir(dir_name) path=os.path.join(settings.MEDIA_ROOT, relative_path_file) if not os.path.exists(path): #如果目录不存在创建目录 os.makedirs(path) file_name=str(uuid.uuid1())+"."+file_suffix path_file=os.path.join(path, file_name) file_url = settings.MEDIA_URL + relative_path_file + file_name open(path_file, 'wb').write(files.file.read()) # 保存图片 return {"error": 0, "url": file_url}
推荐阅读
-
django-blog(七)上传图片
-
webuploader多例上传,按钮替换背景,上传图片替换背景图片,遇到的坑
-
七牛数据处理再添新兵——图片瘦身 图片瘦身CDN七牛云
-
Laravel 将 file 格式的资源利用文件系统上传到七牛云
-
体检管理系统——文件上传功能(七牛云文件上传服务器)
-
七牛云开发-简单的文件上传
-
java对七牛云的简单操作(对象存储文件上传)
-
input选择图片后不上传及时显示 博客分类: js JavaScript图片即显
-
Ajax 实现上传图片即时预览功能 博客分类: ajax上传图片 ajax上传图片ajax图片
-
Ajax 实现上传图片即时预览功能 博客分类: ajax上传图片 ajax上传图片ajax图片