Django noob在这里。我在views.py中有这个视图,它从表单中下载所选文件:
@login_required
def descarga(request):
selected_values = request.POST.getlist('factura')
if request.method == 'POST':
form = Factura.objects.filter(id__in=selected_values)
if form:
book = xlwt.Workbook(encoding='utf8')
sheet = book.add_sheet('report')
sheet.col(0).width = int(13*380)
sheet.col(1).width = int(13*380)
sheet.col(2).width = int(13*380)
sheet.col(3).width = int(13*380)
sheet.col(4).width = int(13*380)
sheet.col(5).width = int(13*380)
sheet.col(6).width = int(13*380)
sheet.col(7).width = int(13*380)
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_LEFT
alignment.vert = xlwt.Alignment.VERT_TOP
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
header_font = Font()
# Header font preferences
header_font.name = 'Times New Roman'
header_font.height = 20 * 15
header_font.bold = True
# Header Cells style definition
header_style = XFStyle()
header_style.font = header_font
font_size_style = xlwt.easyxf('font: name Times New Roman, bold on, height 280; align: wrap on, horz center')
body_font = Font()
# Body font preferences
body_font.name = 'Arial'
body_font.italic = True
borders = Borders()
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
header_style.borders = borders
# body cell name style definition
body_style = XFStyle()
body_style.font = body_font
# 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文件中。问题是,当我点击下载按钮时,我得到的“CSRF令牌丢失或不正确”
我希望有人可以帮助我。任何建议将非常感激。感谢