zappa 为具有多个图像的 Django 表单提供 413 请求实体太大错误
zappa giving 413 request entity too large error for Django form with mutiple images
我有一个 Django 项目,要求用户使用向 post 文章添加多个图像。我正在使用 django 并使用 zappa 部署它。我知道在 Zappa 中页面加载的最大大小是 10mb。我有 1 张 post 图片和 7 张准备图片。当用户添加 7 个大文件然后点击提交时,出现以下错误
HTTP413: PAYLOAD TOO LARGE - The server is refusing to process a
request because the request entity is larger than the server is
willing or able to process.
有没有一种方法可以一张一张地添加图像而不是一次添加所有图像,这样表格就不会损坏,我也不会收到上述错误
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)
然后我有了将多个图像添加到 post
的模型
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)
10 MB 限制不是 Zappa 限制,而是 an API gateway limit。而且,作为 AWS 设置的限制,无法增加它。
您可以通过 API 网关将大量有效负载上传到 S3 的相当复杂的路线。
这篇文章有详细解释:https://sookocheff.com/post/api/uploading-large-payloads-through-api-gateway/
或者您可以做一些前端魔术并发出几个较小的请求,一个用于您上传的每张照片。
我有一个 Django 项目,要求用户使用向 post 文章添加多个图像。我正在使用 django 并使用 zappa 部署它。我知道在 Zappa 中页面加载的最大大小是 10mb。我有 1 张 post 图片和 7 张准备图片。当用户添加 7 个大文件然后点击提交时,出现以下错误
HTTP413: PAYLOAD TOO LARGE - The server is refusing to process a request because the request entity is larger than the server is willing or able to process.
有没有一种方法可以一张一张地添加图像而不是一次添加所有图像,这样表格就不会损坏,我也不会收到上述错误
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)
然后我有了将多个图像添加到 post
的模型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)
10 MB 限制不是 Zappa 限制,而是 an API gateway limit。而且,作为 AWS 设置的限制,无法增加它。
您可以通过 API 网关将大量有效负载上传到 S3 的相当复杂的路线。 这篇文章有详细解释:https://sookocheff.com/post/api/uploading-large-payloads-through-api-gateway/
或者您可以做一些前端魔术并发出几个较小的请求,一个用于您上传的每张照片。