From 4e9f91616977214caf11e0839e445fee468335cd Mon Sep 17 00:00:00 2001 From: Leonhard Strohmidel Date: Thu, 30 Jul 2020 14:15:28 +0200 Subject: [PATCH] Enabling mulitple image upload in 1 form --- .../lostplaces_app/templates/create_place.html | 2 +- lostplaces/lostplaces_app/views.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lostplaces/lostplaces_app/templates/create_place.html b/lostplaces/lostplaces_app/templates/create_place.html index a9a3ae3..6039205 100644 --- a/lostplaces/lostplaces_app/templates/create_place.html +++ b/lostplaces/lostplaces_app/templates/create_place.html @@ -6,7 +6,7 @@ {% block maincontent %}

Place erstellen

-
+ {% csrf_token %} {{ place_form.as_p }} {{ place_image_form.as_p }} diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index e29147b..711aabf 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -4,7 +4,7 @@ from django.views.generic.edit import CreateView from django.views import View from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm -from .models import Place +from .models import Place, PlaceImage # Create your views here. @@ -40,11 +40,22 @@ class PlaceEditView(View): def post(self, request, *args, **kwargs): place_form = PlaceForm(request.POST) - if place_form.is_valid() == True: + if place_form.is_valid(): + submitter = request.user instance = place_form.save(commit=False) # Save logged in user as "submitted_by" - instance.submitted_by = request.user + instance.submitted_by = submitter instance.save() + + if request.FILES: + for image in request.FILES.getlist('filename'): + place_image = PlaceImage.objects.create( + filename=image, + place=instance, + submitted_by=submitter + ) + place_image.save() + kwargs_to_pass = { 'pk': instance.pk }