diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index 711aabf..9466fb3 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -26,11 +26,13 @@ class PlaceEditView(View): def get(self, request, *args, **kwargs): place_image_form = PlaceImageCreateForm() + if 'pk' in kwargs: place = get_object_or_404(Place,pk=kwargs['pk']) place_form = PlaceForm(instance=place) else: place_form = PlaceForm() + context = { 'place_form': place_form, 'place_image_form': place_image_form @@ -42,22 +44,20 @@ class PlaceEditView(View): if place_form.is_valid(): submitter = request.user - instance = place_form.save(commit=False) + place = place_form.save(commit=False) # Save logged in user as "submitted_by" - instance.submitted_by = submitter - instance.save() + place.submitted_by = submitter + place.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() + self._apply_multipart_image_upload( + request.FILES.getlist('filename'), + place, + submitter + ) kwargs_to_pass = { - 'pk': instance.pk + 'pk': place.pk } return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass)) else: @@ -65,3 +65,12 @@ class PlaceEditView(View): 'form': form_place } return render(request, 'create_place.html', context) + + def _apply_multipart_image_upload(self, files, place, submitter): + for image in files: + place_image = PlaceImage.objects.create( + filename=image, + place=place, + submitted_by=submitter + ) + place_image.save()