Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.views.decorators.csrf import csrf_exempt
- from rest_framework.views import APIView
- from rest_framework.exceptions import ParseError
- import io
- import csv
- from rest_framework.response import Response
- #header: es un diccionario con los mismos items que row
- @method_decorator(csrf_exempt, name='dispatch')
- class MassiveUploadView(APIView):
- parser_class = (FileUploadParser,)
- def post(self, request):
- if 'file' not in request.data:
- raise ParseError("Empty content")
- file = request.data['file']
- file = file.read().decode('utf-8')
- file = io.StringIO(file)
- file_reader = csv.DictReader(file)
- for row in file_reader:
- new_row = row.copy()
- for k, v in row.items():
- for k2, v2 in header.items():
- if k == k2:
- new_row[v2] = new_row.pop(k)
- im = Inmueble(**row)
- im.save()
- return Response(status=204)
- -----------------------------------------------------------------------------
- <body>
- <div id="massive">
- <h3>Massive upload</h3>
- <form action="{% url 'muv' %}" enctype="multipart/form-data" id="mass_up_form" method="post">
- Select a file: <input type="file" name="file"><br><br>
- <input type="submit">
- </form>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement