Django Web Tool - upload, process, export

Hello,
I’m in the early stages of Django and I believe I have the fundamentals down however I’m having trouble with my webapp I’m trying to develop. What I would like to do is have a user upload file, then it gets processed using pandas and then the users new file is exported and ready for download. I’ve tried a few things:

views.py

def welcome(request):
    return render(request, "website/welcome.html")


def process_file(file_handle):
    li = []
    df = pd.read_csv(
        file_handle,
        index_col=None,
        header=None,
        error_bad_lines=False,
        sep=",",
        warn_bad_lines=False,
        encoding="latin-1",
    )
    df.columns = ["ID", "Status", "Time", "Date", "Call Letters", " ", " "]
    li.append(df)
    return df.to_csv()


def model_form_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)

        if form.is_valid():
            document = form.save()
            # call to the new method
            csv = process_file(document.document)
            response = HttpResponse(csv, content_type='text/csv')
            response['Content-Disposition'] = 'attachment; filename=result.csv'
            return response
    else:
        form = DocumentForm()
    return render(request, 'website/model_form_upload.html', {'form': form})

models.py

class Document(models.Model):
    document = models.FileField(upload_to='documents/')

resource.py

class DocumentForm(resources.ModelResource):
    class Meta:
        model = Document

template

{% extends 'base.html' %}

{% block content %}
<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Upload</button>
</form>

<p><a href="{% url 'welcome' %}">Return to home</a></p>
{% endblock %}

When I click my upload button, it throws me an error

TypeError: init() takes 1 positional argument but 3 were given

If anyone is able to help me out or even point me in the direction of a good tutorial on what I’m trying to achieve, that would be greatly appreciated

Could you provide more information about the error?. You will have plenty of information in the Web Browser, you can copy and paste it here.

No problem! Here is the traceback error:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/model_form_upload

Django Version: 3.2.6
Python Version: 3.9.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'import_export',
 'website']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\rock.sc\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\rock.sc\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\rock.sc\Desktop\workspace\crm_web_tool\website\views.py", line 36, in model_form_upload
    form = DocumentForm(request.POST, request.FILES)

Exception Type: TypeError at /model_form_upload
Exception Value: __init__() takes 1 positional argument but 3 were given

I’ve never worked with this 3rd-party library you’re using (django-import-export), but from what I can see in the docs, a “ModelResource” is not a form to be created from POST data.

This line doesn’t make sense to me in the context of how you have DocumentForm defined.