django - 如何在 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 按钮时,如何下载它?
解决方案
在 python 中,一个简单的模块非常好用。你可以将它用于任何类型的文件
https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html
推荐阅读
- javascript - 我的函数返回值得到一个空字符串
- sql - 仅选择具有 COLUMN= 的行
如果匹配存在,否则 COLUMN IS NULL - python-3.x - 使用 Pandas 在唯一标识符上连接两个数据框并在列中指定的日期之间求和
- python - Scrapy - 与 start_requests 方法等效的 SitemapSpider
- linux - 关闭 QMenu 时在 Mac OS 和 Linux 上发出的 QEvent 类型的差异
- r - 将计数数据框转换为 R 中的单个观察值行
- python - 表单或模型中的 Django 外键字段验证(仅从外键获取由该用户创建的信息)
- excel - 仅当在用户窗体中单击按钮时才运行“FindNext”
- r - 使用文件路径作为 system() 的参数来执行 C 代码
- javascript - 错误:使用 IBMCloud VisualRecognition NodeJS 未收到响应