0

I'm working on a CRUD app to alter a MySQL database with Django, and I'm trying to add 2 options for a csv upload one that appends data to the table and another one that replaces the data with the uploaded csv ( delete and add) for that I tried to make 2 buttons for a single form but it not working for me.

views.py:

    basecaseform = BaseCaseForm(request.POST)
    BaseCase_columns = [key for key, value in BaseCase.__dict__.items()]
    BaseCase_columns = BaseCase_columns[5:-1]
    basecase = BaseCase.objects.all()
    if request.method == 'GET':
        return render(request,'basecaselist.html',{'basecaseform':basecaseform,'basecase':basecase,'BaseCase_columns':BaseCase_columns})
    if 'replace' in request.GET:
        BaseCase.objects.all().delete()
    else:
        pass
    csv_file=request.FILES['file']
    if not csv_file.name.endswith('.csv'):
        messages.error(request,'this is not a csv file')
    data_set=csv_file.read().decode('UTF-8')
    io_string=io.StringIO(data_set)
    next(io_string)
    for column in csv.reader(io_string,delimiter=',',quoting=csv.QUOTE_NONE):
        _, created=BaseCase.objects.update_or_create(
            base_case_name= column[0],
            version= column[1],
            default= column[2],
        )
    if basecaseform.is_valid():
        basecaseform.save()
    else:
        basecaseform = BaseCaseForm()
    return render(request,'basecaselist.html',{'basecaseform':basecaseform,'basecase':basecase,'BaseCase_columns':BaseCase_columns})

html file :


<form method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <label for="file-upload" class="btn btn-primary">
                 Add
            </label>
            <input id="file-upload" type="file" name="add" onchange="this.form.submit()"/>
             <label for="file-upload" class="btn btn-primary">
                 Replace
            </label>
            <input id="file-upload" type="file" name="replace" onchange="this.form.submit()"/>
        </form>

this solution throw an error :

Exception Type: MultiValueDictKeyError at /basecase/
Exception Value: 'file'
Mehdi Selbi
  • 147
  • 2
  • 11
  • Same question: https://stackoverflow.com/questions/866272/how-can-i-build-multiple-submit-buttons-django-form – Lucas Leite Jan 15 '20 at 16:43
  • Does this answer your question? [How can I build multiple submit buttons django form?](https://stackoverflow.com/questions/866272/how-can-i-build-multiple-submit-buttons-django-form) – dirkgroten Jan 15 '20 at 16:59
  • Note that your error comes from the fact that there is no `` element in your HTML form, therefore `request.FILES['file']` cannot be present. – dirkgroten Jan 15 '20 at 17:02
  • does it work even though I have a file input because I tried it in my case it doesn't work – Mehdi Selbi Jan 16 '20 at 09:26
  • this is the error i get raise ```MultiValueDictKeyError(key) django.utils.datastructures.MultiValueDictKeyError: 'add' ```after trying the answers from the question @LucasLeite @dirkgroten – Mehdi Selbi Jan 16 '20 at 10:35

0 Answers0