Enabling mulitple image upload in 1 form

This commit is contained in:
Leonhard Strohmidel 2020-07-30 14:15:28 +02:00
parent c8380a0073
commit 4e9f916169
2 changed files with 15 additions and 4 deletions

View File

@ -6,7 +6,7 @@
{% block maincontent %} {% block maincontent %}
<h2>Place erstellen</h2> <h2>Place erstellen</h2>
<form method="post"> <form method="post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{{ place_form.as_p }} {{ place_form.as_p }}
{{ place_image_form.as_p }} {{ place_image_form.as_p }}

View File

@ -4,7 +4,7 @@ from django.views.generic.edit import CreateView
from django.views import View from django.views import View
from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm
from .models import Place from .models import Place, PlaceImage
# Create your views here. # Create your views here.
@ -40,11 +40,22 @@ class PlaceEditView(View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
place_form = PlaceForm(request.POST) 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) instance = place_form.save(commit=False)
# Save logged in user as "submitted_by" # Save logged in user as "submitted_by"
instance.submitted_by = request.user instance.submitted_by = submitter
instance.save() 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 = { kwargs_to_pass = {
'pk': instance.pk 'pk': instance.pk
} }