在 django 模板中输出转义字符
我想使用 django 模板系统输出类似 csv 的数据,如下所示:
!connection%block
!dosomething
!dosomethingelse
My,Header,Row,Which,Is,Comma,Seperated
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
!Footerblock
!that has footer information
实际模板如下所示:
!connection%special@oracleconnectionstring
id,account,product,quantity,price,date
{% for r in records %}
{{ r.id }},{{ r.account }},{{ r.product }},{{ r.quantity }},{{ r.price }}
{% endfor %}
!endtransactions
CSV 文档建议使用 csv 模块或模板。我的特殊需求更适合模板,因为某些模型字段需要使用我创建的标签库进行一些操作。
我的观点很简单
result['transactions'] = Transasctions.objects.all()
rendered = render_to_string('trans.csv', result)
response = HttpResponse(rendered , mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=trans.csv'
return response
,但是渲染的模板无法实际输出 DOS CRLF 换行符,因此整个文件是一长行!必须有一种方法可以输出 \r\n 字符 ...
I want to use the django templating system to output csv like data which looks like;
!connection%block
!dosomething
!dosomethingelse
My,Header,Row,Which,Is,Comma,Seperated
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
All,My,Comma,Seperated,Data
!Footerblock
!that has footer information
The actual template looks like:
!connection%special@oracleconnectionstring
id,account,product,quantity,price,date
{% for r in records %}
{{ r.id }},{{ r.account }},{{ r.product }},{{ r.quantity }},{{ r.price }}
{% endfor %}
!endtransactions
The documentation for CSVs recommends either using the csv module or a template. My particular needs are better suited for a template because some of the model fields need some manipulation using a tag library I have created.
My view is simply
result['transactions'] = Transasctions.objects.all()
rendered = render_to_string('trans.csv', result)
response = HttpResponse(rendered , mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=trans.csv'
return response
However the rendered template has no way of actually outputting DOS CRLF newline characters so the entire file is one long line! There must be a way of outputting a \r\n char ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
render_to_string
而不是render_to_response
,并使用普通 TXT 文件而不是 html 文件作为模板。它将保存有关换行符等的数据。Use
render_to_string
instead ofrender_to_response
, and as a template use normal TXT file not html one. It will hold the data about newline chars etc.Django 模板系统通常不会删除换行符。您的模板中有 {% spaceless %} 吗?如果没有,您的 trans.csv 文件中的任何换行符都应该在您的响应中输出。不过,纠正此类空白问题可能很棘手,因为您的空格和缩进模板代码的方式可能与您希望输出间隔的方式不同。
在这里共享 trans.csv 可以更轻松地评论正在发生的事情。
The Django templating system doesn't remove newlines usually. Do you have {% spaceless %} in your template? If not, any newline in your trans.csv file should be output in your response. It can be tricky to get those sorts of whitespace issues correct, though, since the way your space and indent template code may not be the same as how you'd want the output spaced.
Sharing the trans.csv here would make it easier to comment on what's going on.
我建议您使用 csv 模块。您可以更改和定义方言、引用等。如果您选择模板,引用可能会有点棘手。
至于您的问题,请尝试以下操作:
return render_to_response('trans.csv',{'transactions': Transactions.objects.all(), "newline_char": "\n"}, mimetype='text/csv' )
并在模板中:
{%for row in transactions%}{{row.name_of_field1}} {{row.name_of_field2}} {{row.name_of_field3}}... {{newline_char}} {%endfor%}
I recommend you use the csv module. You can change and define dialects, quoting etc. If you choose template, the quoting might be a bit tricky.
As for your problem, try this:
return render_to_response('trans.csv',{'transactions': Transactions.objects.all(), "newline_char": "\n"}, mimetype='text/csv')
and in the template:
{%for row in transactions%}{{row.name_of_field1}} {{row.name_of_field2}} {{row.name_of_field3}}... {{newline_char}}{%endfor%}