Enabling mulitple image upload in 1 form
This commit is contained in:
		@@ -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 }}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user