Saving data from csv file in a database

31 views
Skip to first unread message

Arshdeep Singh

unread,
Jun 3, 2017, 10:15:41 AM6/3/17
to Django users
What I want to do is a user to go to a page where he can add some objects(Model) like a hostel which has some fields, otherwise what he can do is upload a CSV file and what I will do is accept upload of that CSV file and save data in a database. 
I have been trying to do this for past two days searching documentation and StackOverflow, and I did find some helpful links to that but those were not complete and as a result, things did not work out.
I am a newbie in Django So please help me out.
Here are links to some of my searches

1. Given a  file how to store data into database, but problem in this is that how to get path of the file

2.I thought that this would do it, but this answer I feel is not complete and I tried implementing and it is not correct, maybe someone with some experience could interpret what to do, I could not

Also in Link2 I found 
from django.shortcuts import render, redirect
from django.views import View
# Create your views here.

from .forms import DocumentForm
from .models import Document   

class  FileUpload(View):
    def post(self, request):
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            print()
            print(form.cleaned_data['document'].name)
            form.save()
            return redirect('main_db_model:home')
        else:
            return render(request, 'file_upload_form.html', {
                'form': form
            })

    def get(self, request):
        form = DocumentForm()
        return render(request, 'file_upload_form.html', {
            'form': form
        })
I tried searching it in the documentation but I could not find from where did def post: and def get: functions came from. Please help me and I am sorry if i bothered you.


Reply all
Reply to author
Forward
0 new messages