0

Currently I am starting to develop a django project which need to provide a HTML page for other students to upload their experiment results which are excel files (maybe CSV), and save them to databases. But I don't know what should I do with the model.py file for each student have diffenent assignments which means the first row of different experiments are not the same. Can anyone help me?

Django 2.1.7

Qbz
  • 15
  • 4
  • Possible duplicate of [Uploading and processing a csv file in django using ModelForm](https://stackoverflow.com/questions/47491586/uploading-and-processing-a-csv-file-in-django-using-modelform) – Rajiv Sharma Mar 25 '19 at 08:36
  • check this https://stackoverflow.com/questions/47491586/uploading-and-processing-a-csv-file-in-django-using-modelform – Rajiv Sharma Mar 25 '19 at 08:37

1 Answers1

0

try this

in template

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

in forms.py

from django import forms
from .models import YourModel

class YourModelForm(forms.ModelForm):
    class Meta:
        model = YourModel
        fields = ('myfile', )

in views.py

def file_upload(request):
    if request.method == 'POST':
        form = YourModelForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = DocumentForm()
    return render(request, 'template', {
        'form': form
    })

for more details refer this

hope it helps

rahul.m
  • 5,572
  • 3
  • 23
  • 50