静的ファイル (画像、JavaScript、CSS など) の管理Link to this heading
ウェブサイトではふつう、画像や JavaScript、CSS などの追加のファイルを配信する必要があります。Django では、こうしたファイルのことを「静的ファイル (static files)」と呼んでいます。静的ファイルの管理を簡単にするために、Django は django.contrib.staticfiles を提供しています。
このページでは、こうした静的ファイルの配信の仕方について説明します。
静的ファイルの設定Link to this heading
django.contrib.staticfilesが設定ファイルのINSTALLED_APPSに含まれていることを確認してください。設定ファイルの中で、
STATIC_URLを設定します。たとえば、次のようになります。STATIC_URL = '/static/'In your templates, use the
statictemplate tag to build the URL for the given relative path using the configuredSTATICFILES_STORAGE.{% load static %} <img src="{% static "my_app/example.jpg" %}" alt="My image"/>アプリケーション内に
static``というフォルダを作って静的ファイルを保存してください。例えば、``my_app/static/my_app/example.jpgとなります。
プロジェクトには、特定のアプリケーションに紐付けられていない 静的な assets があることがあります。その場合には、アプリケーション内の static/ ディレクトリの他に、設定ファイルでディレクトリのリスト (STATICFILES_DIRS) を定義して、Django が静的ファイルを検索できるようにすることができます。たとえば、次のように設定します。
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
staticfiles がファイルを探索する方法について詳しくは、 STATICFILES_FINDERS のドキュメントを参照してください。
開発時の静的ファイルの取扱いLink to this heading
上で述べたたように django.contrib.staticfiles を利用する場合、 DEBUG が True であれば runserver は自動的にこの処理を行います。もし INSTALLED_APPS 内に django.contrib.staticfiles が存在しない場合は、手動で django.views.static.serve() ビューを用いて静的ファイルを取り扱わなければなりません。
その機能はプロダクション環境で利用するのに適していません! 一般的なデプロイ方法に関しては 静的ファイルのデプロイ を参照ください。
例えば、 STATIC_URL が /static/ として定義される場合、その設定は urls.py に以下のスニペットを追加する事で行えます:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
ユーザーによりアップロードされるファイルの開発時の取扱いLink to this heading
開発中は、ユーザーによってアップロードされたメディアファイルを django.views.static.serve() ビューを利用している MEDIA_ROOT から利用できます。
その機能はプロダクション環境で利用するのに適していません! 一般的なデプロイ方法に関しては 静的ファイルのデプロイ を参照ください。
例えば、 MEDIA_URL を /media/ として設定する場合、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)
テストLink to this heading
When running tests that use actual HTTP requests instead of the built-in
testing client (i.e. when using the built-in LiveServerTestCase) the static assets need to be served along
the rest of the content so the test environment reproduces the real one as
faithfully as possible, but LiveServerTestCase has only very basic static
file-serving functionality: It doesn't know about the finders feature of the
staticfiles application and assumes the static content has already been
collected under STATIC_ROOT.
Because of this, staticfiles ships its own
django.contrib.staticfiles.testing.StaticLiveServerTestCase, a subclass
of the built-in one that has the ability to transparently serve all the assets
during execution of these tests in a way very similar to what we get at
development time with DEBUG = True, i.e. without having to collect them
using collectstatic first.
デプロイLink to this heading
django.contrib.staticfiles provides a convenience management command
for gathering static files in a single directory so you can serve them easily.
Set the
STATIC_ROOTsetting to the directory from which you'd like to serve these files, for example:STATIC_ROOT = "/var/www/example.com/static/"Run the
collectstaticmanagement command:$ python manage.py collectstaticThis will copy all files from your static folders into the
STATIC_ROOTdirectory.Use a web server of your choice to serve the files. 静的ファイルのデプロイ covers some common deployment strategies for static files.
さらに学ぶLink to this heading
This document has covered the basics and some common usage patterns. For
complete details on all the settings, commands, template tags, and other pieces
included in django.contrib.staticfiles, see the staticfiles
reference.