output什么接口(Outputting CSV with Django¶)
Outputting CSV with Django¶
This document explains how to output CSV (Comma Separated Values) dynamically
using Django views. To do this, you can either use the Python CSV library or the
Django template system.Using the template system¶
Alternatively, you can use the Django template system
to generate CSV. This is lower-level than using the convenient Python csv module, but the solution is presented here for completeness.The idea here is to pass a list of items to your template, and have the
template output the commas in a for loop.Heres an example, which generates the same CSV file as above:
The only difference between this example and the previous example is that this
one uses template loading instead of the CSV module. The rest of the code --
such as the mimetype=text/csv -- is the same.Then, create the template my_template_name.txt, with this template code:
This template is quite basic. It just iterates over the given data and displays
a line of CSV for each row. It uses the addslashestemplate filter to
ensure there arent any problems with quotes.Other text-based formats¶
Notice that there isnt very much specific to CSV here -- just the specific
output format. You can use either of these techniques to output any text-based
format you can dream of. You can also use a similar technique to generate
arbitrary binary data; see Outputting PDFs with Django for an example.创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!