0

Django noob here. I have this view in my views.py that downloads the selected files from a form:

@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

In my template i have a Form that contains a table like this:

enter image description here

Under this table i have a button, the user selects the checkboxes and clicks the button And i have to export the selected files into an Excel file. The problem is that when i click the download button i´m getting a "CSRF token missing or incorrect"

I hope someone can help me with this. Any advice will be really appreciated. Thanks

Community
  • 1
  • 1
user3799942
  • 279
  • 2
  • 5
  • 16
  • checkout http://stackoverflow.com/questions/5207160/what-is-a-csrf-token-what-is-its-importance-and-how-does-it-work and related csrf questions. Just a guess, but you're probably missing the token in your template. – monkut Aug 12 '14 at 01:27
  • Hello thanks for the comment. I have the csrf token tag in my template but i still have the problem. – user3799942 Aug 12 '14 at 01:31
  • Take a look around, there are a bunch of csrf related questions that may have answers for you. Here's another: http://stackoverflow.com/questions/8321217/django-csrf-token-missing-or-incorrect – monkut Aug 12 '14 at 01:36
  • Looks like view that shows form and view that processes the form are different. In that case you would get the CSRF error. – Rohan Aug 12 '14 at 04:29

0 Answers0