Running a script with a button on Django

I would like to upload and process “csv” files on Django. My python code is about 100 lines and uses several libraries. I watched your tutorial on uploading files and that was quite helpful. but now I have to figure out how to be able to run my code on the uploaded “csv” file when the user click on a button. What would be the best way to go about this task?


Can you share your code.

My pseudocode goes as follows:

  1. User uploads a csv file
  2. User, then, hits a submit button which triggers a python script
  3. The python script processes the uploaded csv file and outputs another csv file
  4. The output csv file is shown as table on a page

You can do a post request, use something like this on your webpage

        <form method="POST" enctype="multipart/form-data">
        		{% csrf_token %}
                <input class="pull-right" type="file" name="document" id="UploadFile" onchange="ValidateSingleInput(this);" />
            			<button class="btn btn-default pull-right" type="submit" name="excel">Process_excel</button>

    You can then add this in

    def web_page_name(request):
      if request.method =='POST':
          if 'excel' in request.POST:
              uploaded_file = request.FILES["document"]
              #Get the uploaded file and manipulate it and get the changed file and return it as a HTTPresponse
              response = HttpResponse(content_type='application/')
              response['Content-Disposition'] = 'attachment; filename="{}"'.format(excel_file_name)
              xlsx_data = output.getvalue()
              return response
    return render(request, 'web_page_name.html')