3

Have a working app that allows the user to upload multiple files (typically images) at once, but if they upload more than a few files at once, or very large files, it takes very long to go through and they're stuck on the form template waiting while this is happening.

I can tell that what I'm doing isn't very efficient but I'm not sure how to make it better/faster. Am new to django/python, thanks for any help.

views.py:

def add_event(request):
    form = EventFullForm(request.POST or None, request.FILES or None)
    files = request.FILES.getlist('images')
    if request.method == "POST":
        if form.is_valid():
            user = request.user
            title = form.cleaned_data['title']
            description = form.cleaned_data['description']
            start_time = form.cleaned_data['start_time']
            end_time = form.cleaned_data['end_time']
            event_obj = Event.objects.create(user=user, title=title, description=description, start_time=start_time,
                                             end_time=end_time)
            for f in files:
                Images.objects.create(event=event_obj, image=f)
            return redirect('calendarapp:calendar')
        else:
            form = EventFullForm()

    context = {'form': form, 'files': files}
    return render(request, 'calendarapp/test_form.html', context)

Please let me know if sharing any additional code would be helpful

blueblob26
  • 75
  • 9
  • Does this answer your question? https://stackoverflow.com/questions/34006994/how-to-upload-multiple-images-to-a-blog-post-in-django – Anthony Aug 26 '20 at 07:23
  • @alvo it does not. That post helped me to get the above code together and working. This question is asking how to build on that to make the upload process more efficient and user experience better when there are more than just a few images being uploaded at once. – blueblob26 Aug 26 '20 at 12:56

0 Answers0