Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取django中多个复选框的值_Python_Django_Excel_Django Views - Fatal编程技术网

Python 获取django中多个复选框的值

Python 获取django中多个复选框的值,python,django,excel,django-views,Python,Django,Excel,Django Views,我有一张表格,显示不同客户的不同账单: 我的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 f

我有一张表格,显示不同客户的不同账单:

我的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
此函数用于将一张票据的信息下载到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 %}
<input type="checkbox" name="factura" value="{{ faktura.pk }}">
{facturas%中factura的%
{{factura.nombre_cliente}}
{{factura.numero_De_factura}}
{{factura.fecha_factura}}
{%endfor%}

任何小小的帮助都将不胜感激。谢谢

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

{% 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 %}
<input type="checkbox" name="factura" value="{{ faktura.pk }}">

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

为此使用JS,创建所选复选框的数组,提交到视图并返回相应的结果。这听起来不错,但我从未使用过JS。你能展示一些代码让我从中吸取一些想法吗?感谢you@petkostas这里绝对没有JS的理由。正常的HTML可以很好地处理它。@DanielRoseman你是对的Daniel:)谢谢你的回答,一个问题:我应该把支票盒放到表单中吗?因为现在它们只是在一个表的列中,为了让它工作,整个表需要是一种形式。您可以通过查看为管理员变更列表页面生成的HTML来了解它的工作原理:它的设置完全相同,每行的第一列都有一个复选框,整个内容都包装在一个表单中。好的,我明白您的建议,应该可以。最后一个问题:正如您在代码的“数据”部分所看到的,我用事实值(“Cliente”:fact.nombre_Cliente,“Fecha de Factura”:fact.Fecha_Factura,…等)来完成每一列这是因为我正在获取当前事实的id,并从中获取完成列的所有值。我应该如何处理列表?我不太理解你的问题。您需要使用
Factora.objects.filter(id\uu in=selected\u values)
获取与列表对应的Factura对象,然后您可以遍历该查询集,每次输出一行。