简体   繁体   English

多个上传图片Django 2.0

[英]multiple upload image Django 2.0

I should upload more images at the same time, but I can not find a solution, you could help me to understand where I'm wrong. 我应该同时上传更多图片,但是找不到解决方案,您可以帮助我了解我的错误之处。 Thank you 谢谢

models: 楷模:

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

Form: 形成:

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

Views: 观看次数:

 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")

Use one more class to storage your multiples fotos of the same gallery 多使用一个类来存储同一画廊的多个照片

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

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

Than make 1 Form to get all the images, save all imagens in Foto and link it with the GalleriaFotografica that you want 比制作​​1张表格要获取所有图像,将所有图像保存在Foto中并将其与所需的GalleriaFotografica链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM