With this code structure in any way, as far as I understand. We'll have to make a number of changes.
First, change the model so that it can store more than one image:
class Photo(models.Model): image = models.ImageField(upload_to='photos') location = models.ForeignKey(Location, related_name='photos') class Location(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) name = models.CharField(max_length=100, verbose_name="ΠΠΎΠΊΠ°ΡΠΈΡ", default=u'')
Second, change the shape so that it can take more than one image:
class LocationForm(forms.Form): name = forms.CharField(label=u'ΠΠΎΠΊΠ°ΡΠΈΡ') photos = forms.ImageField(label=u'Π€ΠΎΡΠΎΠ³ΡΠ°ΡΠΈΠΈ', widget=forms.FileInput(attrs={'multiple': 'multiple'}))
Finally, complicate the boot algorithm:
from django.shortcuts import render, redirect from django.core.files.base import ContentFile def add_location(request): if request.method == 'GET': form = LocationForm() return render(request, 'add_location.html', {'form': form}) elif request.method == 'POST': form = LocationForm(request.POST, requst.FILES) if form.is_valid(): location = Location.objects.create(user=request.user, name=form.cleaned_data['name']) for f in request.FILES.getlist('photos'): data = f.read() #ΠΡΠ»ΠΈ ΡΠ°ΠΉΠ» ΡΠ΅Π»ΠΈΠΊΠΎΠΌ ΡΠΌΠ΅ΡΠ°Π΅ΡΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ photo = Photo(location=location) photo.image.save(f.name, ContentFile(data)) photo.save() return redirect(location) #ΠΠ°Π΄ΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ get_absolute_url() Π² ΠΌΠΎΠ΄Π΅Π»ΠΈ else: return render(request, 'add_location.html', {'form': form})