Executing Python Script in Django View [Upload CSV -> Batch Geocoding -> Show Link to Download by script generated CSV]


February 2019


0 time


I found the following very useful script while Googleing: https://github.com/shanealynn/python_batch_geocode/blob/master/python_batch_geocoding.py

This script is just what I need and it works flawlessly when I run it locally on my computer. I have a small Django site, where I would like to use this script (non commercially). I do not need a full code example (even though if someone has example code, just post it, I will get trough it) but rather the steps to take to make use of such a script. I googled a lot concerning this the last days and got a lot of different answers and differenct scenarios. Non of them really wholly apply to this scenario though:

  1. Upload CSV
  2. Run Script
  3. Create Link/Make Download possible of newly created CSV

Is anyone out there who might be able to help me out?

Thanks in advance and best regards

edit: Here are the steps and the code I got up until now:

I pretty much followed the following tutorial: https://docs.djangoproject.com/en/2.1/topics/http/file-uploads/

Here a my snippets:


class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField()


def geocode(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            return HttpResponseRedirect('/success/url/')
        form = UploadFileForm()
    return render(request, '../templates/data/geocode/index.html', {'form': form})


urlpatterns = [
path('geocode', views.geocode, name='geocode')]

The File Upload shows up and I am able to pick a file. This is the step where I do not know what to do though. Do I need to save it in my database or can I just load it into the script somehow?


1 answers


You can have your python script in your django project's directory and after the file is uploaded, execute the python script as described here