【问题标题】:Django multiple image upload to S3 BucketDjango 多张图片上传到 S3 Bucket
【发布时间】:2020-06-13 18:42:28
【问题描述】:

我在 Django 中创建了一个简单的电子商务网站,允许用户上传产品的多个图像。这些多张图像需要保存在存储桶 S3 中。

这里是models.py:

class Product(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200, null=True)
    posted_date = models.DateTimeField(default=datetime.now, blank=True)
    size_chart= models.FileField(upload_to='images/', null=True, verbose_name="")

class Product_Pictures(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    file = models.FileField(upload_to='images/', null=True, verbose_name="", default="null") 

这里是views.py:

def add_product(request):
   try:
       files = request.FILES.getlist('pro-image')
       size_chart = request.FILES.get('size_chart', False)

       product = Product.objects.create(user = user, title = request.POST.get('title'), description = request.POST.get('description'), size_chart= size_chart)  
   except Product.DoesNotExist:
       raise Http500()

   product = Product.objects.latest('id')  
   for photo in files: 
       add_product_pictures(dress, photo)    
   return redirect('/ads')

def add_product_pictures(dress, photo):   
    photo = 'images/'+str(photo)
    Dress_Pictures.objects.create(dress =  dress, file = photo)   

因此,当我尝试添加具有多张图片的新产品时,只有一张图片会上传到 S3 存储桶,其他图片都不会上传。虽然条目保存在数据库中并且系统不会引发错误。我尝试了很多方法来管理它,但没有任何效果。我不明白为什么所有文件都没有上传到存储桶。请给我一些出路。

【问题讨论】:

    标签: python django amazon-s3


    【解决方案1】:

    这行代码应该是add_product(dress, files)还是add_product(dress, photos)

    【讨论】:

    • 是的,这是“照片”。我在尝试清理代码时在这里写错了字。
    猜你喜欢
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2022-08-24
    • 2017-11-04
    • 2020-11-11
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多