9

My team has been getting 413 errors whenever we try and upload large files to our Django back-end: 413 Payload too large

We can't exactly pin down the maximum acceptable file size - it seems to vacillate in the 1-3MB range.

Things we have excluded:

  • It's not a webserver configuration issue, because we're running the
    Django server locally (without a webserver)

  • We believe it's not an app server configuration issue, because this happens on multiple app servers (./manage.py runserver and daphne -p 8000 topknott.asgi:application)

  • It's not an issue with the field on the Django model, which looks normal: photo = models.ImageField(blank=True)

Can anyone spot what we're missing?

4

3 回答 3

11

Django 有一个内置机制来防止任何可疑活动。

在你的 settings.py 文件中设置变量

DATA_UPLOAD_MAX_MEMORY_SIZE = 10*1024*1024  # your size limit in bytes

请参阅文档:https ://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DATA_UPLOAD_MAX_MEMORY_SIZE

于 2019-05-28T09:54:14.437 回答
4

如果您的团队曾经/正在使用Django Channels,那么有一段代码会2.1.7导致意外的 413 错误(在此处讨论)。然而,这是固定的2.3.0

于 2019-12-26T20:43:19.733 回答
0

据我所知,runserver 或 daphne 永远不会返回 413。看起来你在 python 服务器前面有 NGINX。

您可以在 nginx.conf 的 server 块中使用 client_max_body_size 更改限制

    server {
        client_max_body_size 20M;
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass         http://127.0.0.1:8000/;
        }
    }
于 2019-03-15T21:23:14.200 回答