在django开发过程中提供用户上传的文件 [英] Serving files uploaded by a user during development in django

查看:152
本文介绍了在django开发过程中提供用户上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但在文档中说:


在开发过程中为用户上传的文件。 p>

在开发期间,您可以使用 django.contrib.staticfiles.views.serve()


MEDIA_ROOT提供用户上传的媒体文件/ code>视图。



这不适合生产使用!对于一些常见的部署
策略,请参阅部署静态文件。



例如,如果您的MEDIA_URL被定义为/ media /,可以这样做
通过添加以下代码段到您的urls.py:

 从django.conf导入设置
从django.conf .urls.static import static

urlpatterns = [
#...其余的URLconf在这里...] + 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 时使用 c> Http404 ,因为应该是您的应用程序部署时的情况。然而,在开发中使用这种方式的好处是这样,您不必运行 collectstatic /收集您的静态文件:您可以从静态或媒体根拔出它们,而无需移动。



这也有点不安全: views.serve()将使用 mimetype 模块,其中将依赖于您正在开发的平台的地图文件:由于特定环境中的mimetype模块,生产中的行为可能不一样。



文档有更多关于 django.contrib.staticfiles.views.serve()



现在,当涉及到生产,服务静态/媒体文件不是Django的WSGI将为您做的很好。 Django是为了提供应用内容而编写的,并且在提供静态/用户上传的内容方面也不会那么好。如果您期望中等负载,请使用单独的服务器,例如 Apache Nginx 与网络服务器(如 uWSGI 更加可持续,可以处理更多。 此文档详细介绍了如何设置/解释更多。


This might be a dumb question but in the documentation it says:

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)

Does that mean for production use + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) should not be used or should be used?

The way I understood this is that you shouldn't use django.contrib.staticfiles.views.serve() but I'm not sure if that's not the same

解决方案

django.contrib.staticfiles.views.serve() should not be used for deployment.

First, calling views.serve() anywhere will only work if DEBUG = True, and will raise an Http404 if used when DEBUG = False, as should be the case when your app is deployed. The benefit of using this in development, however, is such that you don't have to run collectstatic/gather your static files: you can pull them from your static or media root without having to move things around.

It's also somewhat insecure: views.serve() will guess the content type of the the file its serving using the mimetype module, which will rely on the map files for the platform you're developing on: the behavior might not be the same in production because of the mimetype module in your specific environment.

The docs have more on django.contrib.staticfiles.views.serve().

Now when it comes to production, serving static/media files isn't something that Django's WSGI will do well for you. Django is written to serve application content, and won't perform as well when it comes to serving static/user uploaded content. If you're expecting even medium load, using a separate server such as Apache or Nginx in conjunction with a web server like uWSGI is far more sustainable and can handle more. This doc details more on how to set those up/explains more.

这篇关于在django开发过程中提供用户上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆