4

我有一个 Django 项目,需要用户使用将多个图像添加到文章中。我正在使用 django 并使用 zappa 进行部署。我知道在 Zappa 中页面加载的最大大小是 10mb。我有 1 张发布图片和 7 张准备图片。当用户添加 7 个大文件然后点击提交时,我收到以下错误

HTTP413: PAYLOAD TOO LARGE - 服务器拒绝处理请求,因为请求实体大于服务器愿意或能够处理的大小。

有没有一种方法可以将图像一个一个添加而不是一次全部添加,这样表格就不会中断,我也没有收到上述错误

class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')    
    title = models.CharField(max_length=250, unique=True)
    message = models.TextField()
    post_image = models.ImageField(upload_to='post_images/')

    def save(self, *args, **kwargs):
        im = Image.open(self.post_image)
        super(Post, self).save()
        output = BytesIO()
        basewidth = 700
        wpercent = (basewidth / float(im.size[0]))
        hsize = int((float(im.size[1]) * float(wpercent)))
        im = im.resize((basewidth, hsize))
        # after modifications, save it to the output
        im.save(output, format='JPEG', quality=40)
        output.seek(0) 
        self.post_image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.post_image.name.split('.')[0], 'image/jpeg',
                                          sys.getsizeof(output), None)
        super().save(*args, **kwargs)

然后我有向帖子添加多个图像的模型

class Prep (models.Model): #(Images)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='post_prep')
    image = models.ImageField(upload_to='post_prep_images/', blank=True, null=True)
    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        im = Image.open(self.image)
        output = BytesIO()
        basewidth = 700
        wpercent = (basewidth / float(im.size[0]))
        hsize = int((float(im.size[1]) * float(wpercent)))
        super(Prep, self).save()
        im = im.resize((basewidth, hsize))
        # after modifications, save it to the output
        im.save(output, format='JPEG', quality=300)
        output.seek(0)

        # change the imagefield value to be the newley modifed image value
        self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg',
                                        sys.getsizeof(output), None)

        super(Prep, self).save()

在我的 views.py 中,两个模型都被合并以创建 1 个表单

@login_required
def post_create(request):
    ImageFormSet = modelformset_factory(Prep, fields=('image', 'image_title', 'image_description'), extra=7, max_num=7,
                                        min_num=2)
    if request.method == "POST":
        form = PostForm(request.POST or None, request.FILES or None)
        formset = ImageFormSet(request.POST or None, request.FILES or None)
        if form.is_valid() and formset.is_valid():
            instance = form.save(commit=False)
            instance.user = request.user
            instance.save()
            post_user = request.user
            for f in formset.cleaned_data:
                try:
                    photo = Prep(post=instance, image=f['image']
                    photo.save()
                except Exception as e:
                    break
            return redirect('posts:single', username=instance.user.username, slug=instance.slug)
    else:
        form = PostForm()
        formset = ImageFormSet(queryset=Prep.objects.none())
    context = {
        'form': form,
        'formset': formset,
    }
    return render(request, 'posts/post_form.html', context)
4

1 回答 1

3

10 MB 限制不是 Zappa 限制,而是API 网关限制。而且,作为 AWS 设置的限制,没有办法增加它。

您可以走一条相当复杂的路线,通过 API 网关将大型有效负载上传到 S3。本文详细解释:https ://sookocheff.com/post/api/uploading-large-payloads-through-api-gateway/

或者,您可以做一些前端魔术并提出几个较小的请求,为您上传的每张照片发送一个请求。

于 2019-02-18T08:56:41.817 回答