1

I have asked this question before but without successful answer. I hope someone can tell me what exactly is the problem in this.

I´m getting a queryset in my function. It contains multiple objects that come from the selection of the user in a table. This table its contained in a form, that´s how i´m getting all the selected items from the template inside the queryset. This is how i display the items in the table:

{% for factura in facturas %}
    <tr>
        <td><i class="fa fa-file"> <a href="{% url 'ver_Factura' factura.pk %}">Ver</a></i>
            **<div class="checkbox">
                <label>
                    <input type="checkbox" name="factura" value="{{ factura.pk }}">
                </label>
            </div>**
        </td>
        <td>{{ factura.nombre_cliente }}</td>
        <td>{{ factura.numero_De_Factura }}</td>
        <td>{{ factura.fecha_factura }}</td>
    </tr>
{% endfor %}

As you can see they all have a checkbox. The ones selected are the ones that i´m getting for the function in my views.py

The function is this one:

@login_required
def descarga(request):
    **selected_values = request.POST.getlist('factura')**
    if request.method == 'POST':
        **form = Factura.objects.filter(id__in=selected_values)**
        print form
        if form:
            book = xlwt.Workbook(encoding='utf8')
            sheet = book.add_sheet('report')


             # write the header
            header = ['Cliente', 'Fecha de Factura', 'Tipo de Factura', 'Numero de Factura', 'Descripcion', 'Subtotal', 'IVA', 'Precio']

            for hcol, hcol_data in enumerate(header): # [(0,'Header 1'), (1, 'Header 2'), (2,'Header 3'), (3,'Header 4')]
                sheet.write(0, hcol, hcol_data, font_size_style)


            response = HttpResponse(content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment; filename = "report.xls"' 

            for (rownum, facturas) in enumerate(form, start=1):

                data = {
                     "Cliente": facturas.nombre_cliente,
                     "Fecha de Factura":facturas.fecha_factura,
                     "Tipo de Factura": facturas.tipo_Factura,
                     "Numero de Factura": facturas.numero_De_Factura,
                     "Descripcion": facturas.descripcion,
                     "Subtotal": facturas.importe_sin_iva,
                     "IVA": facturas.iva,
                     "Precio": facturas.importe_Total,
                    }

                for column, key in enumerate(header, start=1):
                     sheet.write(1, column, str(data[key]), body_style)


            book.save(response)

            return response

This function exports the data that the user selected into an excel file

What is happening is that i´m getting this error:

Attempt to overwrite cell: sheetname=u'report' rowx=1 colx=1

And i really have no idea how to fix it, i´ve tried changing the numbers of the "start= " and placeing some pdbs to see what really is happening but with no luck.

I really hope someone can point me in the right direction. Thanks

user3799942
  • 279
  • 2
  • 5
  • 16

1 Answers1

1

You always seem to be writing to row 1. You probably mean:

sheet.write(rownum, column, str(data[key]), body_style)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895