簡體   English   中英

如何在 Django 生產環境中提供媒體文件?

[英]How to serve media files on Django production environment?

在我的 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/'

在我的 urls.py 文件中:-

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

當我上傳個人資料圖片時,它正在上傳到指定的文件夾。 但是當我訪問用戶配置文件 url 時,我在終端中遇到這樣的錯誤

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

a_34.png 存在於 /media/profile_images/

那為什么它沒有在瀏覽器上顯示,我收到 404 錯誤?

Django 不是用來在生產環境中提供媒體文件的。 您必須直接從 Web 服務器對其進行配置。

例如

如果您在生產中使用 apache web 服務器,請將以下內容添加到您的虛擬主機配置中

Alias /media/ /path/to/media_file/

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

如果你使用 Nginx,你會有類似的配置。

Django 不鼓勵從服務器提供媒體文件。 使用 amazon s3 等雲服務來管理您的媒體文件。 請參閱此Django 文檔服務媒體,然后在 MEDIA_URL 中提供該路徑。

您可以將 S3 Amazon 用於靜態和媒體文件。 會更好。


S3 亞馬遜的問題

使 S3 存儲桶作為文件系統的一部分出現性能很差並且會隨機失敗。 當我們復制大量文件時,復制完成可能需要 10 分鍾、15 分鍾或 20 分鍾,這使得部署在不需要時需要很長時間。 如果我們將這些直接發送到 S3,相同的復制命令大約需要 1 分鍾才能完成。

解決方案

將 S3BotoStorage 子類化兩次,一類用於靜態文件,另一類用於媒體文件。 這允許我們為每種類型使用不同的存儲桶和子目錄。 (參見:custom_storage.py)

更新設置

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

使用以下內容創建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

根據我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/'

從靜態文件中加載靜態 Django 模板標簽

將模板中{% load static %} 的所有使用更改為{% load static from staticfiles %}

靜態文件中的“靜態”可以為文件使用不同的后端,包括 S3 后端或本地文件后端。 使用“加載靜態”使用不處理不同后端的 Django 模板標簽庫。

在包含靜態文件時以及在包含“static from staticfiles”之后在模板中使用它: {% static “path/to/the/file.ext” %} 這將找出文件的完整路徑,或者它是否在 S3 中它將插入文件的完整 URL。

示例

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

有用信息

“django.contrib.staticfiles.storage.StaticFilesStorage”是默認的Django靜態文件后端

參考文獻

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/

對於 nginx,它適用於我的以下配置行:

location /media {
    alias /home/ubuntu/speedy-net/media; # Change to your own media directory here.
    access_log off;
}

另請參閱關於代碼審查的相關問題

您需要設置一個服務器來在生產中提供靜態內容。 當只有 Debug 為 True 時,靜態內容由 Django 提供。 所以你需要

1)設置服務器

2) 將服務器媒體路徑指向 STATIC_ROOT 目錄

3)運行django的collectstatic命令將所有靜態文件收集到STATIC_ROOT。 請參考

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

以下方法對我有用:

我在 Apache 配置文件中添加了以下配置:

alias /media/ /path/to/media/
<Directory /alchemus/django/WebForm/media>
   Require all granted
</Directory>

settings.py文件包含以下媒體設置:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

確保您在urls.py有以下設置:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

上面的設置是為了確保 django 服務器只在開發過程中提供媒體文件,在生產中提供媒體文件應該由 Apache 服務器處理。

參考資料: https : //docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/#serving-files

只需在 urls.py 中添加此代碼

urlpatterns = […………

   .........

] + 靜態(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

urlpatterns += 靜態(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM