3

I have a TABLE where im showing different bills of different clients:

enter image description here

And i have this code in my views.py:

@login_required
def descarga(request,id_factura):
    selected_values = request.POST.getlist('factura')
    if request.method == 'POST':
        form = Factura.objects.filter(id__in=selected_values)
        if form:

                (...Styling of the Excell file...)

         # 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)

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

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

            response = HttpResponse(mimetype='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment; filename=report.xls'
            response = render_to_response(context_instance = RequestContext(request, locals()), mimetype='application/vnd.ms-excel')
            return response

This function DOwnloads the information of ONE bill into an Excel file. This function is attach to a button that IS NOT in the same template of the table, it´s in the template where the information of he bill is display (by clicking in "Ver" another template shows you the information in a nice format)

So now what i have to do is to place the button in the same page where the table is and export only the ones that are selected in the checkbox.

The question is: How do i tell the view to only export the ones that are checked? AND is this function going to work in the same way in this with as it works in the one wher i´m only showing the information of ONE bill.

This is the code that i have in my template:

{% 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">
           </label>
       </div>
       </td>
       <td>{{ factura.nombre_cliente }}</td>
       <td>{{factura.numero_De_Factura }}</td>
       <td>{{factura.fecha_factura }}</td>
                            </tr>
{% endfor %}

Any little help will be really appreciated. Thank you

user3799942
  • 279
  • 2
  • 5
  • 16

1 Answers1

13

In HTML, every form field needs a name attribute in order to be submitted to the backend. But for checkboxes, you can give them all the same name - but different values - so that they will be submitted as a list. So you can do this in your template:

<input type="checkbox" name="factura" value="{{ faktura.pk }}">

and in the view:

selected_values = request.POST.getlist('factura')

which will give you a list of the selected Factura ids.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for the answer, one question: I shoud put the checboxes in forms? because right now they are just in on column of a table – user3799942 Aug 06 '14 at 15:16
  • For this to work the whole table needs to be in one form. You can see how it would work by looking at the generated HTML for an admin changelist page: it has exactly the same setup, with a single checkbox in the first column of each row, and the whole thing wrapped in a single form. – Daniel Roseman Aug 06 '14 at 15:20
  • OK, i see what you are suggesting, that should work. One last question: As you can see in the "data" part of the code where i complete each column with the value of fact ("Cliente": fact.nombre_cliente, "Fecha de Factura":fact.fecha_factura,...etc) this is because i´m getting the id of the current factura and getting from there all the values to complete the colums. – user3799942 Aug 06 '14 at 15:26
  • How should i do this with the list?. – user3799942 Aug 06 '14 at 15:26
  • I don't really understand your question. You need to get the Factura objects corresponding to your list, with `Factora.objects.filter(id__in=selected_values)`, then you can iterate through that queryset outputting one row each time. – Daniel Roseman Aug 06 '14 at 15:28
  • Hello, im really sorry to boder you but i´m having a problem with a " CSRF token missing or incorrect." Maybe i am doing something wrong in this part: – user3799942 Aug 14 '14 at 18:43
  • response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment;filename=report.xls' response = render_to_response(context_instance = RequestContext(request, locals()), mimetype='application/vnd.ms-excel') return response – user3799942 Aug 14 '14 at 18:43
  • You should post a new question with all the relevant details, if you can't find anything that already solves your problem (there are tons of questions about CRSF tokens already, chances are one of them has the fix). – Daniel Roseman Aug 14 '14 at 20:25
  • Yeah...I posted it but there was no response that helps. Thanks anyway.http://stackoverflow.com/questions/8321217/django-csrf-token-missing-or-incorrect – user3799942 Aug 15 '14 at 18:50