多个上传图片Django 2.0

时间:2018-07-04 14:18:18

标签: django image upload

我应该同时上传更多图片,但是我找不到解决方案,您可以帮助我了解我哪里写错了。谢谢

型号:

class GalleriaFotografica(models.Model):
  user_galleria = models.ForeignKey(User, on_delete=models.CASCADE, 
  related_name="utente_galleria")
  data_inserimento = models.DateTimeField(auto_now_add=True)
  titolo= models.CharField(max_length=250, blank=True, null=True )
  foto=models.ImageField(blank=True, null=True, upload_to = 'gallery/')


  class Meta:
        permissions = (
         ("Azienda", "Azienda"),   

         )
        verbose_name="GalleriaFotografica"
        verbose_name_plural="GalleriaFotografica"


  def __str__(self):
    return self.titolo

表格:

class Form_multimmagine(forms.Form):
 foto = forms.ImageField()

观看次数:

 def foto(request):
    userid=request.user.pk

    if request.method=="POST" and 'caricafoto' in request.POST:
     form=Form_multimmagine(request.POST,  request.FILES)
     if form.is_valid():
        for each in form.cleaned_data['foto']:
            GalleriaFotografica.objects.create(foto=each)
        return HttpResponseRedirect("foto")

1 个答案:

答案 0 :(得分:0)

再使用一个类来存储同一画廊的多张照片

class GalleriaFotografica(models.Model):
  ...
  foto=models.ForeignKey(Foto)

class Foto(models.Model):
  foto=models.ImageField(blank=True, null=True, upload_to = 'gallery/')

比制作1张表格要获取所有图像,将所有图像保存在Foto中并将其与所需的GalleriaFotografica链接