首页 > 解决方案 > 在 Django 查看响应中下载文件后返回消息

问题描述

我正在使用这种方法将 pandas 数据帧下载为 csv 文件:

视图.py ....

        response = HttpResponse(content_type='text/csv')  
        response['Content-Disposition'] = 'attachment; filename="' + filename + '"'
        my_df.to_csv(response, encoding='utf-8', index=False, )
        return response

在我的html中,我有...

<form class="default_form" method="POST" action="do_export">
    {% csrf_token %}
    <br/>
    <input type="submit" class="btn btn-primary exportOut" value="Do Export"
                                       id="myExport"/>
    <br/>
</form>

文件导出工作正常。但是,在导出完成后,我需要能够在 html 页面上显示一条消息 .... 类似 {{ my_message}} 的内容,而无需重新加载页面。

如果这是可行的,我会很感激任何关于如何做到这一点的建议,而不是诉诸 AJAX,我放弃了 AJAX,因为有些下载可能很大。

标签: djangoexport-to-csvmessage

解决方案


在你的 html 文件中使用 message.success (“你的消息在这里”) {% if messages%} {%for msg in messages%}

{{味精}}

{%endfor%} {%endif%}


推荐阅读