获取django中多个复选框的值

时间:2014-08-06 14:40:11

标签: python django excel django-views

我有一个TABLE,其中显示不同客户的不同账单:

enter image description here

我在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

此功能将ONE账单的信息加载到Excel文件中。此功能附加到不在表格的同一模板中的按钮,在模板中显示其账单信息(通过单击“Ver”,另一个模板以漂亮的格式显示信息)

所以现在我要做的是将按钮放在表格所在的同一页面中,只导出复选框中选中的按钮。

问题是:我如何告诉视图只导出已检查的视图?并且这个功能在这方面的工作方式相同,因为它只用于显示一个账单的信息。

这是我模板中的代码:

{% 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 %}

任何小小的帮助都会非常感激。谢谢

1 个答案:

答案 0 :(得分:3)

在HTML中,每个表单字段都需要name属性才能提交到后端。但是对于复选框,您可以为它们提供所有相同的名称 - 但不同的值 - 以便它们将作为列表提交。因此,您可以在模板中执行此操作:

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

并在视图中:

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

将为您提供所选Factura ID的列表。

相关问题