更新时间:2019年07月26日 10时49分23秒 来源:黑马程序员论坛
一、项目背景:二、详细设计: 三、合适的工具: 版本选择: 四、代码详解:
以上这段是官方的自我介绍,说说我个人感受吧。首先这个插件支持批量上传,异步上传等功能,简化大部分JS逻辑方面的代码,具体只要跟着官方的API文档看一看,修改一些参数即可。其次,对于上传时会显示一个进度条,用于显示上传的完成度,这样直观反映了完成度。 4.1、文件上传 HTML代码: <div dir=rtl class="file-loading"> <input id="input-b8" name="input-b8" multiple type="file"></div> JS代码: $(document).ready( function() {$("#input-b8").fileinput({ rtl: true, uploadUrl: '/file_receive/', dropZoneEnabled: false, showPreview: false, allowedFileExtensions: ['txt'], initialPreviewConfig: []});});
后台代码 def file_receive(request): # 接收File-Input空间传送的文件 if request.method == 'POST': file = request.FILES['input-b8'] file_path = "static/books/"+file.name with open(file_path,"wb") as f: for chunk in file.chunks(): f.write(chunk) return JsonResponse({'status':'success'})
拓展: 4.2、异步更新已上传的文件列表 HTML代码: <div style="padding-top: 20px"> <table id="book_list" class="table table-striped table-bordered table-hover"> <tr> <th>上传书籍</th> <th>上传时间</th> <th>文件大小</th> <th>操作</th> </tr> {% for book in objects %} <tr> <td>{{ book.name}}</td> <td>{{ book.book_time }}</td> <td>{{ book.book_size }}</td> <td><a href="/book_read/?book_name={{ book.name }}">阅读</a> <a href="/book_del/?book_name={{ book.name }}">删除</a></td> </tr> {% endfor %} </table></div> JS代码: $("#input-b8").on('fileuploaded',function(){ console.log('success'); $.get('/book_update/',function(data){ var book_html ="<tr>\n" + "<th>上传书" + "籍</th>" + "<th>上传时间</th>" + "<th>文件大小</th>" + "<th>操作</th>"+ "</tr>"; console.log(data); for (var i in data){ book_html += "<tr><td>"+ data['name']+"</td>" + "<td>"+data['book_time']+"</td>" + "<td>"+data['book_size']+"</td>" + "<td><a href=\"/book_read/?book_name="+data['name']+"\">阅读</a>"+ "<a href=\"/book_del/?book_name="+data['name']+"\">删除</a></td>"+ "</tr>" } $("#book_list").html(book_html) console.log(book_html) });});
后台代码 def book_list(): # 获取books目录下的书籍 file_list = [] filedir_path = "static/books/" list_file = os.listdir(filedir_path) for book in list_file: book_info = {} book_path = filedir_path + book book_info['name'] = book book_info['timestamp'] = os.path.getctime(book_path) book_info['book_time'] = time_format(book_info['timestamp']) book_info['book_size'] = os.path.getsize(book_path) file_list.append(book_info) books = sorted(file_list,key= lambda x:x['timestamp'],reverse=True) return books def time_format(timestamp): # 格式化时间戳成指定的时间 time_struct = time.localtime(timestamp) time_string = time.strftime('%Y-%m-%d %H:%M',time_struct) return time_string 4.3、文章分页模块 HTML代码: <div class="header text-center "> <a href="/index/" style="float: left;"> <i class="fa fa-home fa-2x" aria-hidden="true">Home</i> </a> <h3>{{ book_name }}</h3></div><div class="col-md-12 col-sm-offset-1 main"> {% for content in book_content %} <span>{{ content }}</span> {% endfor %}</div><div class="pagination"> <div class="col-md-4 "> {% if book_content.has_previous %} <i class="fa fa-arrow-left" aria-hidden="true"> <a href="?book_name={{ book_name }}&page={{ book_content.previous_page_number }}"> 上一页 </a> </i> {% endif %} </div> <div class="col-md-4 "> <h5> 第{{ book_content.number }}页/共{{ book_content.paginator.num_pages }}页 </h5> </div> {% if book_content.has_next %} <div class="col-md-4 "> <a href="?book_name={{book_name}}&page={{ book_content.next_page_number }}"> 下一页 </a> <i class="fa fa-arrow-right" aria-hidden="true"> </i> </div> {% endif %}</div> JS代码: def book_read(request): # 获取上传书籍的内容 if request.method == 'GET': book_name = request.GET['book_name'] # 书籍名称 file_path = "static/books/" + book_name # 书籍路径 with open(file_path,encoding='gbk', errors='ignore') as f: book_contents = f.readlines() paginator = Paginator(book_contents, 50) try: page = int(request.GET['page']) # 页码 book_content = paginator.page(page) except Exception as e: book_content = paginator.page(1) return render_to_response('book.html',{'book_content': book_content, 'book_name': book_name})
拓展: 3、Paginator对象操作:实例化对象:book_list = [1,2,3,4,5,6,7,8]book_content = Paginator(book_list,3)取特定页的数据content = book_content.page(2)查特定页当前页码数:content.number查分页后的总页数content.num_pages查询某一页是否有上一页或者查询上一页页码:content.has_previous()content.previous_page_number()查询某一页是否有下一页或者查询下一页页码:content.has_next()content.next_page_number() |
推荐了解热门学科
java培训 | Python人工智能 | Web前端培训 | PHP培训 |
区块链培训 | 影视制作培训 | C++培训 | 产品经理培训 |
UI设计培训 | 新媒体培训 | 产品经理培训 | Linux运维 |
大数据培训 | 智能机器人软件开发 |
传智播客是一家致力于培养高素质软件开发人才的科技公司,“黑马程序员”是传智播客旗下高端IT教育品牌。自“黑马程序员”成立以来,教学研发团队一直致力于打造精品课程资源,不断在产、学、研3个层面创新自己的执教理念与教学方针,并集中“黑马程序员”的优势力量,针对性地出版了计算机系列教材50多册,制作教学视频数+套,发表各类技术文章数百篇。
传智播客从未停止思考
传智播客副总裁毕向东在2019IT培训行业变革大会提到,“传智播客意识到企业的用人需求已经从初级程序员升级到中高级程序员,具备多领域、多行业项目经验的人才成为企业用人的首选。”
中级程序员和初级程序员的差别在哪里?
项目经验。毕向东表示,“中级程序员和初级程序员最大的差别在于中级程序员比初级程序员多了三四年的工作经验,从而多出了更多的项目经验。“为此,传智播客研究院引进曾在知名IT企业如阿里、IBM就职的高级技术专家,集中研发面向中高级程序员的课程,用以满足企业用人需求,尽快补全IT行业所需的人才缺口。
何为中高级程序员课程?
传智播客进行了定义。中高级程序员课程,是在当前主流的初级程序员课程的基础上,增加多领域多行业的含金量项目,从技术的广度和深度上进行拓展。“我们希望用5年的时间,打造上百个高含金量的项目,覆盖主流的32个行业。”传智播客课程研发总监于洋表示。
黑马程序员热门视频教程【点击播放】
Python入门教程完整版(懂中文就能学会) | 零起点打开Java世界的大门 |
C++| 匠心之作 从0到1入门学编程 | PHP|零基础入门开发者编程核心技术 |
Web前端入门教程_Web前端html+css+JavaScript | 软件测试入门到精通 |