0

I have a template that displays some information (this information changes everytime i open the template because shows data from different bills so it changes all the time) That data is render like this in a table:

<table id="items">
    <tr>
        <th class="tipo">Tipo de Factura</th>
        <th class="descripcion">Descripcion</th>
        <th>Precio</th>
    </tr>
    <tr class="item-row">
        <td><div><textarea>{{fact.tipo_Factura}}</textarea></div></td>
        <td class="description"><textarea>{{fact.descripcion}}</textarea></td>
        <td><span class="price">$ {{fact.importe_sin_iva}}</span></td>
    </tr>

</table>
<table id="totales">
    <tr>
        <td class="total-line">Subtotal</td>
        <td class="total-value"><div id="subtotal">$ {{fact.importe_sin_iva}}</div></td>
    </tr>
    <tr>
        <td class="total-line">Iva</td>
        <td class="total-value"><div id="total">$ {{iva}}</div></td>
    </tr>
    <tr>
        <td  class="total-line">Precio Total</td>
        <td class="total-value"><textarea id="paid">$ {{total}}</textarea></td>
    </tr>
</table>

So now i have to place a bottom under the table and when the user press it the information has to be downloaded into an Excel file and be saved somewhere in the users computer.

Does someone have any idea or can point in any (right) direction to see how this can be done?

Any advice would be appreciated. Thank you

user3799942
  • 279
  • 2
  • 5
  • 16

1 Answers1

1

I have following code in similar place:

"place a bottom" - is link, like this:

<a href="/some/path/report"></a>

in urls.py:

...
url(r'^/some/path/report$', file_load_view),
...

in view.py

from StringIO import StringIO
from csv import DictWriter

@require_http_methods(["GET"])
def file_load_view(self, request):
    f = StringIO()
    writer = DictWriter(f, ["Tipo de Factura", "Descripcion", "Precio", "Subtotal", "total", "paid"])
    writer.writeheader()
    report_line = {
        "Tipo de Factura": fact.tipo_Factura,
        "Descripcion": fact.descripcion,
        ...
    }
    writer.writerow(report_line)
    report = f.getvalue()
    resp = HttpResponse(report, mimetype="application/octet-stream")
    resp["Content-Disposition"] = "attachment; filename='{}'".format("report.csv")
    return resp

As result user load csv file (it is almost the same as 'exel') contains report like this:

Tipo de Factura,Descripcion,Precio,Subtotal,total,paid
1,2,3,4,5,6 

If you already have report in file on your disk report may equal:

...
fd = open("report/path/report.csv")
report = fd.read()
...
sheh
  • 1,003
  • 1
  • 9
  • 31
  • Hello so what should i add in the report fiel? Im sorry my english is not so good – user3799942 Jul 29 '14 at 18:21
  • @Nacho I added sample how you may build `report` – sheh Jul 30 '14 at 06:29
  • This is an amazing answer. Really. Just one more question: how can i parse the result. Now im getting everything in two lines and is unreadeable(i don´t know if that´s a word...). I really appreciate all the help. – user3799942 Jul 31 '14 at 02:23
  • You can open `report.csv` using Microsoft Excel this make the file more readable. Also you can try create the file using [xlwt](http://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet) and use `mimetype="application/vnd.ms-excel"` but I not try this. – sheh Jul 31 '14 at 06:20