Saturday, June 3, 2017

Saving data from csv file in a database

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
https://stackoverflow.com/questions/2459979/how-to-import-csv-data-into-django-models

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
 https://stackoverflow.com/questions/42544523/how-do-i-get-a-files-absolute-path-after-being-uploaded-in-django

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.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/624102ad-126c-4cf3-845a-7eb3bddbe001%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment