fix: make sure error is displaed for invalid form and owner is added

This commit is contained in:
moanos [he/him] 2024-08-08 16:48:51 +02:00
parent 00fe8d8ab9
commit f8ca384b41

View File

@ -223,7 +223,10 @@ def add_photo_to_animal(request, animal_id):
form = ImageForm(request.POST, request.FILES) form = ImageForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
instance = form.save() instance = form.save(commit=False)
instance.owner = request.user
instance.save()
animal.photos.add(instance) animal.photos.add(instance)
if "save-and-add-another" in request.POST: if "save-and-add-another" in request.POST:
form = ImageForm(in_flow=True) 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) form = ImageForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
instance = form.save() instance = form.save(commit=False)
instance.owner = request.user
instance.save()
adoption_notice.photos.add(instance) adoption_notice.photos.add(instance)
if "save-and-add-another" in request.POST: if "save-and-add-another" in request.POST:
form = ImageForm(in_flow=True) form = ImageForm(in_flow=True)
return render(request, 'fellchensammlung/forms/form-image.html', {'form': form}) return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
else: else:
return redirect(reverse("adoption-notice-detail", args=[adoption_notice_id])) return redirect(reverse("adoption-notice-detail", args=[adoption_notice_id]))
else:
return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})
else: else:
form = ImageForm(in_flow=True) form = ImageForm(in_flow=True)
return render(request, 'fellchensammlung/forms/form-image.html', {'form': form}) return render(request, 'fellchensammlung/forms/form-image.html', {'form': form})