美文网首页
day8 数据上传和进度条

day8 数据上传和进度条

作者: LittleBear_6c91 | 来源:发表于2019-05-05 11:27 被阅读0次
    image.png

    hr9/models.py

    from django.db import models
    
    class Comic(models.Model):
        comic_id = models.CharField(max_length=32)
        title = models.CharField(max_length=128)
    

    hr9/views.py

    from django.shortcuts import render
    
    # Create your views here.
    from django.shortcuts import render
    from django.http import JsonResponse
    import xlrd
    from hr9.models import Comic
    import threading
    
    
    def hr_list(request):
        context = {'message': '', 'rows_count': 0}
        return render(request, 'hr/hr_list.html', context)
    
    def read_excel_count(filename):
        workbook = xlrd.open_workbook(filename)
        sheet1 = workbook.sheet_by_name('Sheet1')
        rows_count = sheet1.nrows - 1
        return rows_count
    
    def get_count(filename):
        upload_count = Comic.objects.all().count()
        json_data = {'upload_count': upload_count}
        return JsonResponse(json_data)
    
    def read_excel(filename):
        workbook = xlrd.open_workbook(filename)
        sheet1 = workbook.sheet_by_name('Sheet1')
        rows_count = sheet1.nrows
    
        # 遍历excel列表
        for row_num in range(1, rows_count):
            row = sheet1.row(row_num)
            comic_id = row[1].value
            title = row[2].value
    
            comic = Comic()
            comic.comic_id = comic_id
            comic.title = title
            comic.save()
    
        return rows_count
    
    def upload_excel(request):
        hr_excel = request.FILES.get("hr_excel", None)
        filename = 'C:\\zz\hr_excel.xlsx'
        with open(filename, 'wb') as f:
            f.write(hr_excel.read())
    
        rows_count = read_excel_count(filename)
    
        read_excel_thread = threading.Thread(target=read_excel, args=(filename,))
        read_excel_thread.start()
    
    
        context = {'message': '上传成功', 'rows_count': rows_count}
        return render(request, 'hr/hr_list.html', context)
    

    settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    'django.contrib.staticfiles',
        'hr9',
    

    oa9/urls.py

    """oa URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.1/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path
    from hr9.views import hr_list, upload_excel, get_count
    
    urlpatterns = [
        path('hr_list/', hr_list),
        path('upload_excel/', upload_excel),
        path('get_count/', get_count),
    ]
    

    templates/hr_list.html

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="/static/js/jquery-3.3.1.min.js"></script>
    </head>
    <body>
    <h1>请上传人力资源excel文件</h1>    
    <h2>{{ message }}  总记录数:{{rows_count}} </h2>
    <h2>当前导入条数:<span id="upload_count">0</span></h2>
    
    <div style="height: 55px; width: 600px; border: solid green 1px;">
        <div id="progress_bar" style="background-color: red; height: 50px; width: 0px;">
        </div>
    </div>
    
        <form id="upload_form" name="upload_form" action="/upload_excel/" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="file" name="hr_excel" value="">
            <input type="submit" name="submit_button" value="提交">
        </form>
    
    <table border="1">
        {% for item in result_list %}
        <tr><td>{{ item }}</td></tr>
        {% endfor %}
    </table>
    
    
        <script> 
        function get_count(){ 
            var rows_count = {{rows_count}};
            if (rows_count == 0) {
                return ;
            }
            
            $.get('/get_count/', function (data) {
                upload_count = data.upload_count;
                var percent = upload_count / rows_count;
                $('#upload_count').text(data.upload_count);
                $('#progress_bar').css('width', parseInt(598 * percent));
            });
        } 
        //重复执行某个方法 setInterval重复执行
        var t1 = window.setInterval(get_count, 1000); 
        // var t2 = window.setInterval("hello()",3000); 
        </script> 
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:day8 数据上传和进度条

          本文链接:https://www.haomeiwen.com/subject/qtecoqtx.html