首页 > 解决方案 > 如何在 Django 中下载 ExcelDownload

问题描述

如何在 Django 中下载 ExcelDownload

我的模块.py

**modules.py**
def getAnswerStats(year, month):
    answer_stats = Lawyer.objects.raw(''' select
        select * from (
            select 
                y.lawyer_name,
                DATE_FORMAT(y.register_date, '%Y-%m-%d') as reg_date,
                (select count(counsel_answer_idx) from counsel_answer where lawyer_idx = y.lawyer_idx and DATE_FORMAT(register_date, '%Y%m') = '%s') as cnt,
                (select count(counsel_answer_idx) from counsel_answer where lawyer_idx = y.lawyer_idx and DATE_FORMAT(register_date, '%Y%m') = '%s' and week(register_date,5) - week(DATE_SUB(register_date,INTERVAL DAYOFMONTH(register_date)-1 DAY),5) = 1) as cnt1
        where y.lawyer_status = 'N'
        ) A order by A.cnt desc ''', (year+month, year+month, year+month, year+month, year+month, year+month, year+month))

    return answer_stats

admin_view.py

@csrf_exempt
def answer_stats_excel(request):

    if request.method == "POST":
        year = request.POST.get("year")
        month = request.POST.get("month")
        print(year+month)

        stats_data = getAnswerStats(year, month)

    return redirect('/admin/visual/answer_stats')

stats_data 是从模块中检索的。

answer_stats.html

    <form id="boardFrm" name="boardFrm" enctype="multipart/form-data" action="/admin/visual/answer_stats_excel" method="post">
        <div class="form-inline">
            <select class="form-control " style="width:49%" id="year" name="year"></select>
            <select class="form-control " style="width:49%" id="month" name="month"></select>
        </div>
            <button type="submit" style="margin-top:20px;" name="button"> excel down</button>
        </form>

ExcelDownload button here 当我单击 Exceldown 按钮时,如何下载它?

标签: djangodjango-admin

解决方案


在 python 中,一个简单的模块非常好用。你可以将它用于任何类型的文件

https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html


推荐阅读