1

I have a django website with a database. On the site, you can add, change, delete information from the database. I made a button that, when clicked, downloads a .csv file. I open it in excel and it looks something like this:

enter image description here

The data set does not matter, I have a different one, I took this one on the Internet for an example.

I want that when I click on the "download report" button, I would create a table at least like in this example:

enter image description here

Is it possible? At the moment, my views.py contains the following code to get data from the database and download it to an excel spreadsheet:

def export_racks_view(request, id):
    response = HttpResponse(content_type='text/csv')
    response.write(u'\ufeff'.encode('utf8'))
    writer = csv.writer(response, delimiter=';', dialect='excel')
    writer.writerow([
        'Rack number',
        'Name',
        'Amount',
        'Vendor',
        'Model',
        'Describe',
        'numbering',
        'responsible',
        'financially responsible person',
        'inventory number',
        'row',
        'place',
        'height',
        'width',
        'depth',
        'unit width',
        'unit depth',
        'rack type',
        'place type',
        'max load',
        'power sockets',
        'power sockets UPS',
        'external ups',
        'cooler',
        'updated by',
        'updated at',
        'room name',
        'building name',
        'site name',
        'department name',
        'region name',
    ])
    raw_report = Rack.objects.raw("""select rack.id as id,  rack.rack_name,     rack.rack_amount,   rack.rack_vendor,   rack.rack_model,    rack.rack_description,  rack.numbering_from_bottom_to_top,  rack.responsible,   rack.rack_financially_responsible_person,   rack.rack_inventory_number,     rack.row,   rack.place,     rack.rack_height,   rack.rack_width,    rack.rack_depth,    rack.rack_unit_width,   rack.rack_unit_depth,   rack.rack_type,     rack.rack_place_type,   rack.max_load,  rack.power_sockets,     rack.power_sockets_ups,     rack.external_ups,  rack.cooler,    rack.updated_by,    rack.updated_at,    room.room_name,     building.building_name,     site.site_name,     department.department_name,     region.region_name  from rack   join room on    room.id =   rack.room_id_id     join building on    building.id =   room.building_id_id     join site on    site.id =   building.site_id_id     join department on  department.id =     site.department_id_id   join region on  region.id =     department.region_id_id where region.id ="""+str(id)+""";""")
    for rack in raw_report:
        if rack.numbering_from_bottom_to_top == True:
            numbering_from_bottom_to_top = 'Yes'
        else:
            numbering_from_bottom_to_top = 'No'
        if rack.external_ups == True:
            external_ups = 'Yes'
        else:
            external_ups = 'No'
        if rack.cooler == True:
            cooler = 'Yes'
        else:
            cooler = 'No'
        writer.writerow([
            rack.id,
            rack.rack_name,
            rack.rack_amount,
            rack.rack_vendor,
            rack.rack_model,
            rack.rack_description,
            numbering_from_bottom_to_top,
            rack.responsible,
            rack.rack_financially_responsible_person,
            rack.rack_inventory_number,
            rack.row,
            rack.place,
            rack.rack_height,
            rack.rack_width,
            rack.rack_depth,
            rack.rack_unit_width,
            rack.rack_unit_depth,
            rack.rack_type,
            rack.rack_place_type,
            rack.max_load,
            rack.power_sockets,
            rack.power_sockets_ups,
            external_ups,
            cooler,
            rack.updated_by,
            rack.updated_at,
            rack.room_name,
            rack.building_name,
            rack.site_name,
            rack.department_name,
            rack.region_name,
        ])
    response['Content-Disposition'] = 'attachment; filename="racks.csv"'
    return response
  • Haven't you already achieved it? – 911 Jun 30 '23 at 06:28
  • No, I download a table in the form as in the first picture, but I would like it to be downloaded with graphic design, as in the second picture – user21402031 Jun 30 '23 at 06:31
  • Are you referring to adding some bold dividing lines and orange padding to the table? – 911 Jun 30 '23 at 06:39
  • Yes, that's right, make the table so that it has bold borders, change the background of the cells, center alignment, in general, make it beautiful – user21402031 Jun 30 '23 at 06:45
  • Then you can't use .csv files, you need to use a file format like xlsx, and you also need to use pandas, openpyxl library to achieve. Could you give me a link to download the csv, I can help you – 911 Jun 30 '23 at 06:52
  • I have a django site running on a server on my local network, I'm afraid I can't provide a direct link. I understand that I need to change my view from csv to xlsx. Do the pandas and openpyxl libraries need to be downloaded from the Internet, or do you just need to do an import in the django project? My server has no internet access, only LAN. – user21402031 Jun 30 '23 at 06:59
  • So how did the Django library come from? – 911 Jun 30 '23 at 07:09
  • If you can't connect to the Internet, how is python installed? How is the django package downloaded? – 911 Jun 30 '23 at 07:15
  • I made a project on a computer that had Internet access, created everything that was needed, then transferred it to a local server (this process is not as simple as I write here), then I finalized the project through a remote connection to the server, but just recently got to uploading reports. They are uploaded, everything works, but .csv is an ugly format, I thought maybe there is a way to somehow interact with the cell background color and table borders through python code – user21402031 Jun 30 '23 at 07:15
  • In this case, you need to download pandas and openpyxl and transfer them again, they are not python built-in packages. – 911 Jun 30 '23 at 07:19
  • It will be very difficult, most likely, I will have to abandon this idea :( – user21402031 Jun 30 '23 at 07:23
  • I think there are some problems with your way of development. You can't guarantee that there won't be any other python dependencies after the project. Once you do, it will cause you a lot of trouble – 911 Jun 30 '23 at 07:44

0 Answers0