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 ...