【问题标题】:return message after file download in Django view response在 Django 查看响应中下载文件后返回消息
【发布时间】:2019-12-01 17:38:37
【问题描述】:

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

view.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,因为有些下载可能很大。

【问题讨论】:

    标签: django export-to-csv message


    【解决方案1】:

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

    {{msg}}

    {%endfor%} {%endif%}

    【讨论】:

    • 我已经有这个编码了。它不会按原样工作......提交表单后我需要一些东西来触发事件。
    • 如果 yoi 使用类基础视图,那么只有 successMessagemixin 可以工作。
    猜你喜欢
    • 2011-08-15
    • 2022-11-11
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多