CSV modifying app

Hi everyone!

Had a more general question. I want to make an app that you upload a .csv file to, then the app generates a new csv, then you download it.

What would be the best approach for this?

Would you recommend using models, or is there a simple way to use POST without models? I’ve had a look at the documentation but it is a bit beyond me. I’d appreciate any advice on the general approach you can give. Thanks

Sad man

Are you going to want to keep a copy of the csv on the server? Or is it just a transient processing of data?

If the latter, then a model is definitely not necessary. See File Uploads | Django documentation | Django to get started, and then read Uploaded Files and Upload Handlers | Django documentation | Django.

Your view would display a form with a FileField. On submit, your view would accept a file, read it, generate the output file, and then (possibly) return a FileResponse.

1 Like

Hi @KenWhitesell ,

Thanks so much for your answer. It would be transient processing. Thanks for your help.

So I’ve been trying your suggestion. However, I have two problems. First, I can’t seem to select the file from the request.FILES. I keep getting a MultiValueDictKeyError.

Here are my files:

views.py

def formatter(request):
    form = ArrayCsvForm()
    return render(request, "lab/formatter.html", {'form':form})

def formatter_result(request):
    my_file = request.FILES['myfile']

forms.py

from django import forms 
from .models import ArrayCsv


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

formatter.html

{% extends 'lab/base.html' %}
{% load static %}
{% block content %}

<div class="resources-page">

    <form action="{% url 'lab:formatter_result' %}" method="POST"  enctype="multipart/form-data" name='myfile' type='file'>
        {% csrf_token %}
        {{form.as_p}}
        <button type="submit" name='myfile' >Upload and Submit</button>
    </form>
</div>

{% endblock %}

formatter_results.html

{% extends 'lab/base.html' %}
{% load static %}
{% block content %}

<div class="resources-page">

    <form action="{% url 'lab:formatter_result' %}" method="POST"  enctype="multipart/form-data" name='myfile' type='file'>
        {% csrf_token %}
        {{form.as_p}}
        <button type="submit" >Upload and Submit</button>
    </form>
</div>

{% endblock %}

Second, when I get around it using this view instead, how can I use this file since it is in a InMemoryUploadedFile, and not a csv?

def formatter_result(request):


    for filename, file in request.FILES.items():
        name = request.FILES[filename]
        my_file = file

Sorry for the long question Mr Whitesell, but any help would be appreciated, I’m really struggling with this. Thanks again for your help so far.

Your views here don’t match the patterns of code as shown in the docs I referenced.

I suggest you start simple by following the docs as closely as possible before changing the patterns being used in the code, until you get to the point where you’re understanding what’s going on.

1 Like

Ok thank you @KenWhitesell