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