在DjangoAdmin中使用KindEditor(上传图片)
2024-08-31 14:10:25
一、下载
http://kindeditor.net/down.php
删除asp、asp.net、php、jsp、examples文件夹
拷贝到static目录下
二、配置
kindeditor目录下新建conifg.js
KindEditor.ready(function(K) {
K.create('#id_content', {
width: '800px',
height: '500px',
uploadJson:'/upload_image',
});
//http://kindeditor.net/docs/option.html
});
admin.py
class NoticeAdmin(admin.ModelAdmin):
class Media:
js = [
'/static/plugins/kindeditor/kindeditor-all-min.js',
'/static/plugins/kindeditor/lang/zh-CN.js',
'/static/plugins/kindeditor/config.js'
]
admin.site.register(Notice, NoticeAdmin)
views.py
from django.views.decorators.csrf import csrf_exempt @csrf_exempt
def upload_image(request):
allow_suffix = ['jpg', 'png', 'jpeg', 'gif', 'bmp']
files = request.FILES.get("imgFile", None)
if files:
file_suffix = files.name.split(".")[-1]
if file_suffix not in allow_suffix:
return JsonResponse({"error": 1, "message": "图片格式不正确"})
file_name = str(uuid.uuid1()) + "." + file_suffix
file_path = os.path.join(settings.MEDIA_ROOT, 'image', file_name)
url_path = os.path.join(settings.MEDIA_URL, 'image', file_name)
print file_path
with open(file_path, 'wb') as f:
f.write(files.file.read())
return JsonResponse({"error": 0, "url": url_path})
settings.py
MEDIA_URL = "/upload/"
MEDIA_ROOT = os.path.join(BASE_DIR, "upload")
urls.py
from django.views.static import serve
url(r'^upload/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT,}),
最新文章
- YOURPHP的分页完整版
- yii2的GridView和ActiveDataProvider具体使用
- PostgreSQL没有redo log multiplexing
- 《javascript权威指南》第9章 例9-8源码
- loadrunner参数化总结
- CodeIgniter入门——HelloWorld
- android项目--上下文菜单
- php curl调用相关api
- Dom 简介
- SpringMVC简单入门
- 使用异步方法在XAML中绑定系统时间
- Android 平台 Native 代码的崩溃捕获机制及实现
- shell 基础(二)变量
- JavaScript 环境和作用域
- [SF] Symfony 标准 HttpFoundation\Request 实现分析
- Centos 7.x nginx隐藏版本号
- MySQL Json类型的数据处理
- 学习笔记(2)——实验室集群LVS配置
- HDU 4502 吉哥系列故事——临时工计划(一维动态规划)
- $#65279导致页面body会出现一个空白行
热门文章
- css解决fixed布局不会出现滚动条问题
- Spark + sbt + IDEA + HelloWorld + MacOS
- CRLF——http response 拆分攻击(webgoat)
- go module配置
- CentOS 7.5在线安装Docker 18.09.3
- 【leetcode算法-简单】14. 最长公共前缀
- Oracle表级约束和列级约束
- Chrome浏览器控制台报 POST http://*** net::ERR_BLOCKED_BY_CLIENT
- 码云以及Git的使用
- GAN——ModeCollapse