I need to upload cvs data to database and I wrote the code which did it. but I need to do it triggered: every time user upload file - the python script update the database. In my code: I have call_command('uploadDb') when I place it not in function upload - it works, but when I place it inside the upload function - it didnt What is the problem? Thank you
enter code here
from django.shortcuts import render
import os
from django.core.management import call_command
Create your views here. (file views.py)
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Upload the file</h1>")
def home(request):
return render(request, 'index.htm', {'what':'File Upload'})
def upload(request):
if request.method == 'POST':
handle_uploaded_file(request.FILES['file'], str(request.FILES['file']))
return HttpResponse("Successful")
return HttpResponse("Failed")
def handle_uploaded_file(file, filename):
if not os.path.exists('upload/'):
os.mkdir('upload/')
with open('upload/' + filename, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
call_command('uploadDb')
Aucun commentaire:
Enregistrer un commentaire