简体   繁体   English

Django-如何在生产中为用户上传的内容提供服务

[英]Django - How to serve user uploaded content in production

I have written a small ecommerce app in Django. 我用Django编写了一个小型电子商务应用程序。 The admin user will be uploading photos of the products. 管理员用户将上传产品照片。 I got this working on my dev machine by specifying the following handler in urls.py 我通过在urls.py中指定以下处理程序在我的开发机上工作

if settings.DEBUG:
urlpatterns += patterns('django.views.static',
    (r'media/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}),
)

In my Product model I have the following method returning the image url: 在我的产品模型中,我可以通过以下方法返回图片网址:

def detail_thumbnail_url(self) : return  self.detail_thumbnail.url

And from my template: 从我的模板:

<img src="{{ product.detail_thumbnail_url }}">

There are warnings everywhere on not making Django handle this in production, but nowhere can I find how to handle this in production. 到处都有关于不让Django在生产环境中处理此问题的警告,但我无处可寻到如何在生产环境中处理此问题。 Surely someone must have done this! 当然一定有人这样做!

I want to serve the images from the same server that's running the web app. 我想从运行Web应用程序的同一台服务器提供图像。

As you may have read, that approach is not recommended. 您可能已经读过,不建议采用这种方法。 You must use a webserver along with your django app and connect them via WSGI . 您必须将Web服务器与django应用程序一起使用,并通过WSGI将它们连接起来。 You've got basically three options…. 您基本上有三个选择……。


Configure and use your webserver 配置和使用您的Web服务器

This means you need to install a webserver (Nginx recommended) and configure it along with WSGI interfaces like Gunicorn or uWSGI . 这意味着你需要安装一个网络服务器(Nginx的推荐),并与像WSGI接口配置它沿着GunicornuWSGI This option gives you maximum freedom and will help you understand how things work under the hood. 此选项为您提供最大的自由度,并且将帮助您了解事物在幕后的工作方式。

If you want to develop locally with all this config and skip the burden of installation and configuration you can use pre-baked Vagrant VMs for development. 如果您想使用所有此配置在本地进行开发,而无需承担安装和配置的负担,则可以使用预烘焙的Vagrant VM进行开发。


Use a PaaS solution 使用PaaS解决方案

Deploying your app to Heroku , ElasticBeanstalk , GAE, etc or your own PaaS (heroku-style git deployments) with docker/dokku . 部署你的应用程序的HerokuElasticBeanstalk用,GAE等或者你自己的PaaS(Heroku的风格git的部署) 泊坞窗/ dokku This solution will handle most of the the configurations for you. 该解决方案将为您处理大多数配置。 It will save you time (and money). 这将节省您的时间和金钱)。


Serving from a CDN 从CDN提供服务

It would be much better if you serve the statics from a CDN, no matter if you deploy to PaaS or your machines. 如果从CDN服务于静,不管你这将是更好的 部署到PaaS的或您的机器。 For instance you could programatically upload the user pics and media directly to AWS S3 with the help of django apps like django-s3direct or make your own with boto… (no need to reinvent the wheel tho). 例如,您可以在django-s3direct之类的django应用程序的帮助下以编程方式将用户图片和媒体直接上传到AWS S3,或者使用boto自己制作……(无需重新发明轮子)。

This option will ease the load of your server and speed up your website. 此选项将减轻服务器的负载并加快网站速度。

You do it by configuring your actual server - Apache, or whatever - to serve files from wherever MEDIA_ROOT is, on whatever MEDIA_URL is. 您可以通过配置实际的服务器-Apache或其他服务器来实现,以便从MEDIA_ROOT所在的任何位置(无论MEDIA_URL所在的位置)提供文件。 Django should have nothing to do with it. Django应该与它无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM