【问题标题】:Get the values of multiple checkboxes in django获取django中多个复选框的值
【发布时间】:2014-08-06 14:40:11
【问题描述】:

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

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

此功能D将一张账单的信息下载到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 %}

任何一点帮助都将不胜感激。谢谢

【问题讨论】:

  • 为此使用 JS,创建一个选中复选框的数组,提交到视图并返回适当的结果。
  • 听起来不错,但我从未使用过 js。你能显示任何代码,以便我可以从中获得一些想法吗?谢谢
  • @petkostas 绝对没有理由在这里使用 JS。普通的 HTML 可以很好的处理。
  • @DanielRoseman 你是对的丹尼尔 :)

标签: python django excel django-views


【解决方案1】:

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

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

在视图中:

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

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

【讨论】:

  • 感谢您的回答,一个问题:我应该把复选框放在表单中吗?因为现在他们只是在表格的列上
  • 为此,整个表格需要采用一种形式。您可以通过查看为管理员更改列表页面生成的 HTML 来了解它是如何工作的:它具有完全相同的设置,在每行的第一列中只有一个复选框,并且整个内容都包含在一个表单中。跨度>
  • 好的,我明白你的建议,应该可以。最后一个问题:正如您在代码的“数据”部分中看到的那样,我用事实值完成每一列(“Cliente”:fact.nombre_cliente,“Fecha de Factura”:fact.fecha_factura,...等) 这是因为我正在获取当前事实的 id 并从那里获取所有值以完成列。
  • 我应该如何处理这个列表?
  • 我不太明白你的问题。您需要使用Factora.objects.filter(id__in=selected_values) 获取与您的列表相对应的 Factura 对象,然后您可以遍历该查询集,每次输出一行。
猜你喜欢
  • 2017-09-11
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
相关资源
最近更新 更多