From f8ca384b416dfd2359cdc63af59eefdcd3d101b5 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 8 Aug 2024 16:48:51 +0200 Subject: [PATCH] fix: make sure error is displaed for invalid form and owner is added --- src/fellchensammlung/views.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 3594144..1517312 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -223,7 +223,10 @@ def add_photo_to_animal(request, animal_id): form = ImageForm(request.POST, request.FILES) if form.is_valid(): - instance = form.save() + instance = form.save(commit=False) + instance.owner = request.user + instance.save() + animal.photos.add(instance) if "save-and-add-another" in request.POST: form = ImageForm(in_flow=True) @@ -244,13 +247,17 @@ def add_photo_to_adoption_notice(request, adoption_notice_id): form = ImageForm(request.POST, request.FILES) if form.is_valid(): - instance = form.save() + instance = form.save(commit=False) + instance.owner = request.user + instance.save() adoption_notice.photos.add(instance) if "save-and-add-another" in request.POST: form = ImageForm(in_flow=True) return render(request, 'fellchensammlung/forms/form-image.html', {'form': form}) else: return redirect(reverse("adoption-notice-detail", args=[adoption_notice_id])) + else: + return render(request, 'fellchensammlung/forms/form-image.html', {'form': form}) else: form = ImageForm(in_flow=True) return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})