refactor: Allow image upload for animals
This commit is contained in:
@@ -4,7 +4,7 @@ from django.urls import reverse
|
||||
import markdown
|
||||
|
||||
from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image
|
||||
from .forms import AdoptionNoticeForm, AnimalForm
|
||||
from .forms import AdoptionNoticeForm, AnimalForm, ImageForm
|
||||
|
||||
|
||||
def index(request):
|
||||
@@ -47,26 +47,30 @@ def add_adoption(request):
|
||||
|
||||
def add_animal_to_adoption(request, adoption_notice_id):
|
||||
if request.method == 'POST':
|
||||
form = AnimalForm(request.POST, request.FILES)
|
||||
form = AnimalForm(request.POST)
|
||||
image_form = ImageForm(request.POST, request.FILES)
|
||||
|
||||
if form.is_valid():
|
||||
form.cleaned_data["adoption_notice_id"] = adoption_notice_id
|
||||
instance = form.save(commit=False)
|
||||
instance.adoption_notice_id = adoption_notice_id
|
||||
|
||||
if 'image' in request.FILES:
|
||||
image_instance = Image(image=request.FILES['image'])
|
||||
image_instance.save()
|
||||
instance.photos.add(image_instance)
|
||||
|
||||
instance.save()
|
||||
|
||||
if 'image_-image' in request.FILES:
|
||||
image = Image(image=request.FILES['image_-image'])
|
||||
image.save()
|
||||
instance.photos.add(image)
|
||||
|
||||
if "button_add_another_animal" in request.POST:
|
||||
return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)]))
|
||||
else:
|
||||
return redirect(reverse("adoption-notice-detail", args=[str(adoption_notice_id)]))
|
||||
else:
|
||||
form = AnimalForm()
|
||||
return render(request, 'fellchensammlung/form_add_animal_to_adoption.html', {'form': form})
|
||||
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
|
||||
return render(request, 'fellchensammlung/form_add_animal_to_adoption.html',
|
||||
{'form': form, "image_form": image_form})
|
||||
|
||||
|
||||
def about(request):
|
||||
|
Reference in New Issue
Block a user