Django - Let the user select multiple database entries and use that data in the next page

I would like to create a ListView, where the user can select certain database entries (see image below).

ListView with checkboxes (not yet connected to a form)

The data connected to these entries would then be used to make plots. How do I create a form in which items of the ListView can be selected?

So far I am able to create extra database entries in the CsvCreateView, but I don't know how to make a form in the ListView, where you can select the entries. Can anyone help me?

The code:

models.py:

class CsvCreate(models.Model):
file_name = models.FileField(upload_to='csvs')
uploaded = models.DateTimeField(auto_now_add=True)
selected = models.BooleanField(default=False)

def __str__(self):
    return f"File id: {self.id}, uploaded at {self.uploaded}, activated: {self.activated}"

def save_csvs(self):
    obj = self
    with open(obj.file_name.path, 'r') as f:
        reader = csv.reader(f)
        values = [obj]
        dict_list = []
        for i, row in enumerate(reader):
            if i < 3:
                pass
            else:
                for i in range(0,len(row)):
                    values.append(row[i])
                data_dict = dict(zip(parameters.parameters, values))
                dict_list.append(data_dict)
                values = [obj]
        django_list = [Data(**vals) for vals in dict_list]
        Data.objects.bulk_create(
            django_list
        )
        obj.activated = True
        obj.save()

views.py:

class CsvCreateView(View):
model = CsvCreate
form_class = CsvModelForm
success_url = reverse_lazy('csvs:list_csvs')
template_name = 'csvs/upload.html'

def get(self, request):
    form = self.form_class()
    context = {'form': form}
    return render(request, self.template_name, context)

def post(self, request):
    form = self.form_class(request.POST or None, request.FILES or None)
    if form.is_valid():
        csv_create = form.save()
        csv_create.save_csvs()
    else:
        print(form.errors)
    return redirect(self.success_url) 

class CsvListView(ListView):
    # CsvCreate_list.html
    model = CsvCreate
    context_object_name = 'csv_list'
Back to Top