我正在尝试在我的 View 中提供一个文本文件,但我似乎无法让 Django 尊重文本文件中的换行符。代码是:
response = TemplateResponse(request, template='dashboard/email_template.txt', mimetype='text/plain', status=200)
response['Content-Disposition'] = 'attachment; filename=mail_template.txt'
return response
文件看起来像:
key : {{ user.profile.secret_key }} # secret key
message: blah blah # Your message
提供文件下载时,这两行会连接起来...当我使用任何浏览器、文本编辑器或 IDE 打开模板 (email_template.txt) 时,都会考虑换行符。
是什么让 Django(如果这与 Django 有关?)连接这些行,我该如何阻止它?
提前致谢!
最佳答案
这实际上都是记事本的错,但您可以通过 unix2dos
将模板转换为使用 Windows 风格的换行符来修复它。
关于django - 如何防止 Django 的响应框架剥离文本文件的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879826/