静的ファイル (画像、JavaScript、CSS など) の管理Link to this heading

ウェブサイトではふつう、画像や JavaScript、CSS などの追加のファイルを配信する必要があります。Django では、こうしたファイルのことを「静的ファイル (static files)」と呼んでいます。静的ファイルの管理を簡単にするために、Django は django.contrib.staticfiles を提供しています。

このページでは、こうした静的ファイルの配信の仕方について説明します。

静的ファイルの設定Link to this heading

  1. django.contrib.staticfiles が設定ファイルの INSTALLED_APPS に含まれていることを確認してください。

  2. 設定ファイルの中で、STATIC_URL を設定します。たとえば、次のようになります。

    Code
    STATIC_URL = '/static/'
    
  3. テンプレート中で、静的ファイルへの URL を /static/my_app/myexample.jpg のようにハードコーディングするか、または、より望ましい方法として static テンプレートタグを記述して、設定した STATICFILES_STORAGE ストレージを使うことにより、指定した相対パスから URL を動的に構築します (この方法は特に、静的ファイルを配信する CDN を切り替えたくなった時に、作業を簡単にしてくれます)。

    Django template
    {% load static %}
    <img src="{% static "my_app/example.jpg" %}" alt="My image"/>
    
  4. アプリケーション内に static``というフォルダを作って静的ファイルを保存してください。例えば、``my_app/static/my_app/example.jpg となります。

プロジェクトには、特定のアプリケーションに紐付けられていない 静的な assets があることがあります。その場合には、アプリケーション内の static/ ディレクトリの他に、設定ファイルでディレクトリのリスト (STATICFILES_DIRS) を定義して、Django が静的ファイルを検索できるようにすることができます。たとえば、次のように設定します。

Code
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
]

staticfiles がファイルを探索する方法について詳しくは、 STATICFILES_FINDERS のドキュメントを参照してください。

Serving static files during developmentLink to this heading

If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True. If you don't have django.contrib.staticfiles in INSTALLED_APPS, you can still manually serve static files using the django.contrib.staticfiles.views.serve() view.

This is not suitable for production use! For some common deployment strategies, see 静的ファイルのデプロイ.

For example, if your STATIC_URL is defined as /static/, you can do this by adding the following snippet to your urls.py:

Code
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)

Serving files uploaded by a user during developmentLink to this heading

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 静的ファイルのデプロイ.

For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:

Code
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.

DeploymentLink 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.

  1. Set the STATIC_ROOT setting to the directory from which you'd like to serve these files, for example:

    Code
    STATIC_ROOT = "/var/www/example.com/static/"
    
  2. Run the collectstatic management command:

    Code
    $ python manage.py collectstatic
    

    This will copy all files from your static folders into the STATIC_ROOT directory.

  3. 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.