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

Refresh

February 2019

Views

0 time

1

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:

forms.py

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

views.py

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

urls.py

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?

Tim

1 answers

1

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