在 django 开发期间提供用户上传的文件
Serving files uploaded by a user during development in django
这可能是一个愚蠢的问题,但在文档中它说:
Serving files uploaded by a user during development.¶
During development, you can serve user-uploaded media files from
MEDIA_ROOT using the django.contrib.staticfiles.views.serve()
view.
This is not suitable for production use! For some common deployment
strategies, see Deploying static files.
For example, if your MEDIA_URL is defined as /media/, you can do this
by adding the following snippet to your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是否意味着生产用途 + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
不应该使用或应该使用?
我的理解是你不应该使用 django.contrib.staticfiles.views.serve()
但我不确定这是否不一样
django.contrib.staticfiles.views.serve()
不应用于部署。
首先,在任何地方调用 views.serve()
仅在 DEBUG = True
时才有效,如果在 DEBUG = False
时使用,则会引发 Http404
,您的应用程序应该是这种情况已部署。然而,在开发中使用它的好处是您不必 运行 collectstatic
/收集静态文件:您可以从静态或媒体根目录中提取它们而无需移动东西左右。
它也有些不安全:views.serve()
会使用 mimetype
模块猜测其服务的文件的内容类型,这将依赖于您正在开发的平台的地图文件: 由于您特定环境中的 mimetype 模块,生产中的行为可能不同。
文档有更多关于 django.contrib.staticfiles.views.serve()
的内容。
现在,当涉及到生产时,提供 static/media 文件并不是 Django 的 WSGI 能为您做好的事情。 Django 是为提供应用程序内容而编写的,在提供 static/user 上传内容方面表现不佳。如果您期望中等负载,请使用单独的服务器,例如 Apache or Nginx in conjunction with a web server like uWSGI is far more sustainable and can handle more. This doc 详细说明如何设置这些 up/explains。
在生产中,您应该有 Debug = False
。当您这样做时,Django 将不再提供静态文件。
一种流行的替代方法是使用 Amazon 的 S3 Buckets。出于示例目的,假设您创建了一个新存储桶。您的 MEDIA_URL
看起来像:
MEDIA_URL = 'bucket.s3.amazonaws.com/media'
您将以相同的方式提供静态文件。
这是否回答了您的问题?
这可能是一个愚蠢的问题,但在文档中它说:
Serving files uploaded by a user during development.¶
During development, you can serve user-uploaded media files from MEDIA_ROOT using the
django.contrib.staticfiles.views.serve()
view.This is not suitable for production use! For some common deployment strategies, see Deploying static files.
For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:
from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是否意味着生产用途 + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
不应该使用或应该使用?
我的理解是你不应该使用 django.contrib.staticfiles.views.serve()
但我不确定这是否不一样
django.contrib.staticfiles.views.serve()
不应用于部署。
首先,在任何地方调用 views.serve()
仅在 DEBUG = True
时才有效,如果在 DEBUG = False
时使用,则会引发 Http404
,您的应用程序应该是这种情况已部署。然而,在开发中使用它的好处是您不必 运行 collectstatic
/收集静态文件:您可以从静态或媒体根目录中提取它们而无需移动东西左右。
它也有些不安全:views.serve()
会使用 mimetype
模块猜测其服务的文件的内容类型,这将依赖于您正在开发的平台的地图文件: 由于您特定环境中的 mimetype 模块,生产中的行为可能不同。
文档有更多关于 django.contrib.staticfiles.views.serve()
的内容。
现在,当涉及到生产时,提供 static/media 文件并不是 Django 的 WSGI 能为您做好的事情。 Django 是为提供应用程序内容而编写的,在提供 static/user 上传内容方面表现不佳。如果您期望中等负载,请使用单独的服务器,例如 Apache or Nginx in conjunction with a web server like uWSGI is far more sustainable and can handle more. This doc 详细说明如何设置这些 up/explains。
在生产中,您应该有 Debug = False
。当您这样做时,Django 将不再提供静态文件。
一种流行的替代方法是使用 Amazon 的 S3 Buckets。出于示例目的,假设您创建了一个新存储桶。您的 MEDIA_URL
看起来像:
MEDIA_URL = 'bucket.s3.amazonaws.com/media'
您将以相同的方式提供静态文件。
这是否回答了您的问题?