在 django 模板中输出转义字符

发布于 2024-09-25 06:25:54 字数 1091 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2024-10-02 06:25:54

使用 render_to_string 而不是 render_to_response,并使用普通 TXT 文件而不是 html 文件作为模板。它将保存有关换行符等的数据。

Use render_to_string instead of render_to_response, and as a template use normal TXT file not html one. It will hold the data about newline chars etc.

绝不服输 2024-10-02 06:25:54

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.

网名女生简单气质 2024-10-02 06:25:54

我建议您使用 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%}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文