On Wednesday, October 25, 2017 at 2:49:30 AM UTC-5, Jonathon wrote:
[...]
Hi Jonathon and the group. I found this:
===
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.
Hereâs an example, which generates the same CSV file as above:
from django.http import HttpResponse
from django.template import loader, Context
def some_view(request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;
filename="somefilename.csv"'
# The data is hard-coded here, but you could load it from a database or
# some other source.
csv_data = (
('First row', 'Foo', 'Bar', 'Baz'),
('Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"),
)
t = loader.get_template('my_template_name.txt')
c = Context({
'data': csv_data,
})
response.write(t.render(c))
return response
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 content_type='text/csv' â is the same.
Then, create the template my_template_name.txt, with this template code:
{% for row in data %}"{{ row.0|addslashes }}", "{{ row.1|addslashes }}",
"{{ row.2|addslashes }}", "{{ row.3|addslashes }}", "{{ row.4|addslashes }}"
{% endfor %}
This template is quite basic. It just iterates over the given data and
displays a line of CSV for each row. It uses the addslashes template filter
to ensure there arenât any problems with quotes.
====
It is out of my expetise to even try the solution. Is this the only way now?
Regards
Lin
--
---
You received this message because you are subscribed to the Google Groups "Mayan EDMS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mayan-edms+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.