如何在django生产环境中提供媒体文件?

[英]How to serve media files on django production evironment?


In me settings.py file :-

在我的settings.py文件中: -

DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'
LOGIN_URL = '/login/'
MEDIA_URL = '/media/'

In my urls.py file:-

在我的urls.py文件中: -

urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

When I am uploading the profile image then it is uploading to specified folder. but when i am visiting the user profile url then i am getting error like this in terminal

当我上传个人资料图片时,它会上传到指定的文件夹。但是当我访问用户个人资料网址时,我在终端中收到这样的错误

"GET /media/profile_images/a_34.jpg HTTP/1.1" 404 103

a_34.png is present in /media/profile_images/

a_34.png存在于/ media / profile_images /中

then why it is not showing on browser and i am getting 404 error?

那为什么它没有在浏览器上显示我得到404错误?

4 个解决方案

#1


4  

Django is not made to serve media file in production environment. You must configure it directly from the web server.

Django不是为了在生产环境中提供媒体文件。您必须直接从Web服务器进行配置。

For example

If you are using apache web server in production, add the below to your virtualhost configuration

如果您在生产中使用Apache Web服务器,请将以下内容添加到您的虚拟主机配置中

Alias /media/ /path/to/media_file/

<Directory /path/to/media_file/>
Order deny,allow
Allow from all
</Directory>

If you use Nginx you would have similar configuration.

如果您使用Nginx,您将拥有类似的配置。

#2


3  

Django discourages to serve media files on production from the server. Use cloud services like amazon s3 to server your media files. See this Django doc serve media then give that path in MEDIA_URL.

Django不鼓励从服务器上提供生产中的媒体文件。使用像amazon s3这样的云服务来为您的媒体文件提供服务。看到这个Django doc serve media然后在MEDIA_URL中提供该路径。

#3


1  

You need to setup a server to serve static content on production. When only Debug is True, static content is served by Django. So you need to

您需要设置服务器以在生产中提供静态内容。当只有Debug为True时,Django会提供静态内容。所以你需要

1) Setup a server

1)设置服务器

2) Point server media path to STATIC_ROOT directory

2)将服务器媒体路径指向STATIC_ROOT目录

3) Run collectstatic command of django to collect all the static files to STATIC_ROOT. Please refer

3)运行django的collectstatic命令将所有静态文件收集到STATIC_ROOT。请参考

https://docs.djangoproject.com/en/1.10/howto/static-files/

#4


1  

You can use S3 Amazon for static and media files. It will be better.

您可以将S3 Amazon用于静态和媒体文件。会好起来的。


Problem with S3 Amazon

S3亚马逊的问题

Making the S3 bucket appear as part of the file system has terrible performance and fails randomly. When we are copying a lot of files it can take 10, 15, or 20 minutes for the copying to complete making deployments take a long time when they don’t need to. If we send these directly into S3 the same copy command takes about 1 minute to complete.

使S3存储桶作为文件系统的一部分出现具有可怕的性能并且随机失败。当我们复制大量文件时,复制完成需要10分钟,15分钟或20分钟才能完成,因此部署需要很长时间才能完成。如果我们将这些直接发送到S3,则相同的复制命令大约需要1分钟才能完成。

Solution

Subclass S3BotoStorage twice, one class for static files and the other for media files. This allows us to use different buckets and subdirectories for each type. (see: custom_storage.py)

子类S3BotoStorage两次,一个用于静态文件,另一个用于媒体文件。这允许我们为每种类型使用不同的存储桶和子目录。 (参见:custom_storage.py)

Update settings

1. AWS_STORAGE_BUCKET_NAME  needs to be bucket to hold static files and media files
2. MEDIAFILES_BUCKET
3. MEDIAFILES_LOCATION
4.DEFAULT_FILE_STORAGE
5.STATICFILES_BUCKET
6.STATICFILES_LOCATION
This is the subdirectory under the S3 bucket for the app
7.STATIC_URL
8.STATICFILES_STORAGE

Create custom_storage.py with the contents:

使用以下内容创建custom_storage.py:

from django.utils.deconstruct import deconstructible
from storages.backends.s3boto import S3BotoStorage
from django.conf import settings

@deconstructible
class StaticS3Storage(S3BotoStorage):
   bucket_name = settings.STATICFILES_BUCKET
   location = settings.STATICFILES_LOCATION

@deconstructible
class MediaS3Storage(S3BotoStorage):
   bucket_name = settings.MEDIAFILES_BUCKET
   location = settings.MEDIAFILES_LOCATION

Sample settings.py.tmpl for updates settings (as mentioned above) based on my stack.json

使用我的stack.json对settings.py.tmpl示例进行更新设置(如上所述)

MEDIAFILES_BUCKET = '<%= @node["apps_data"]["aws"]["buckets"]["bucket-name"] %>'
MEDIAFILES_LOCATION = 'folder_name_for_media_files_in_bucket'
DEFAULT_FILE_STORAGE = 'custom_storage.MediaS3Storage'

# If we're not using our S3 backend storage we need to serve the media files via path
   if DEFAULT_FILE_STORAGE == "custom_storage.MediaS3Storage":
       MEDIA_URL = 'https://%s.s3-website-us-east-1.amazonaws.com/%s/' %      (MEDIAFILES_BUCKET, MEDIAFILES_LOCATION)
   else:
       MEDIA_URL = '/media/'

   STATICFILES_BUCKET = '<%= @node["apps_data"]["aws"]["buckets"]["bucket-name"] %>'
   STATICFILES_LOCATION = 'folder_name_for_static_files_in_bucket'
   STATICFILES_STORAGE = '<%= @node["deploy_data"]["project_name"]["django_static_files_storage"] %>'

# If we're not using our S3 backend storage we need to serve the static files via path
   if STATICFILES_STORAGE == "custom_storage.StaticS3Storage":
       STATIC_URL = 'https://%s.s3-website-us-east-1.amazonaws.com/%s/' % (STATICFILES_BUCKET, STATICFILES_LOCATION)
   else:
       STATIC_URL = '/static/'

load static from staticfiles Django Template Tag

Change all uses of {% load static %} in templates to {% load static from staticfiles %}

将模板中{%load static%}的所有用途更改为{%从staticfiles%加载静态}

The “static” from static files can make use of different back ends for files, including an S3 back end or local file back end. Using “load static” uses the Django template tags library which doesn’t handle different back ends.

静态文件中的“静态”可以使用不同的文件后端,包括S3后端或本地文件后端。使用“load static”使用Django模板标签库,它不处理不同的后端。

Use this in the templates when including a static file and after including “static from staticfiles”: {% static “path/to/the/file.ext” %} This will figure out the full path to the file or if it’s in S3 it will insert a full URL to the file.

在包含静态文件时以及在包含“static from staticfiles”之后在模板中使用它:{%static“path / to / the / file.ext”%}这将找出文件的完整路径或者它是否在S3中它会在文件中插入完整的URL。

Example

<link rel="stylesheet" type="text/css" href="{% load static from staticfiles %}{% static "css/style.css" %}”>

Useful info

“django.contrib.staticfiles.storage.StaticFilesStorage” is the default Django static files backend

“django.contrib.staticfiles.storage.StaticFilesStorage”是默认的Django静态文件后端

References

https://docs.djangoproject.com/en/1.9/howto/static-files/ https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/08/20/7cb5644eabeb6ddc42cad2587fdc062a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告