... lp you manage them. このページでは、こうした静的ファイルの配信の仕方について説明します。 静的ファイルの設定 django.contrib.staticfiles が設定ファイルの INSTALLED_APPS に含まれていることを確認してください。 設定ファイルの中で、 STATIC_URL を設定します。 ... たとえば、次のようにします: STATIC_URL = "static/" テンプレート内では、 static テンプレートタグを使用して、 staticfiles STORAGES で設定したエイリアスで相対パ ... スの URL を生成できます。 {% load static %} < img src = " {% static 'my_app/example.jpg' %} " alt = "My image" > アプリ内に static というフォルダを作成って静的ファ ... ... スのアセットリストは、このプロパティから取得できます: >>> w = CalendarWidget () >>> print ( w . media ) <link href="https://static.example.com/pretty.css" media="all" rel="stylesheet"> <script src="https://static.example.com/animations.js"></script> <sc ... ript src="https://static.example.com/actions.js"></script> 以下は指定可能な Media オプションです。必須のものはありません。 css 様々な形式の出力メデ ... s" ], "print" : [ "newspaper.css" ], } 上記のCSS定義をレンダリングすると、次のようなHTMLになります: < link href = "https://static.example.com/pretty.css" media = "screen" rel = "stylesheet" > < link href = "https://static.example.com/lo_res.css" media ... ... ルをデプロイする を参照してください。 設定 次の設定に関する詳細については、 静的ファイルの設定 を参照してください。 STORAGES STATIC_ROOT STATIC_URL STATICFILES_DIRS STATICFILES_FINDERS 管理コマンド django.contrib.staticfiles は3つの管理コマンドを提供しま ... す。 collectstatic django-admin collectstatic 静的ファイルを STATIC_ROOT に収集します。 デフォルトでは、重複するファイル名はテンプレートの解決 ... すなわち、指定された場所のいずれかで最初に見つかったファイルが使用されます。どのファイルが反映されたかわからない場合は、 findstatic コマンドを使用して、どのファイルが適用されたかを確認できます。 その後、再度 collectstatic を実行すると、( STATIC_ROOT が空 ... ... ンダリングするために、画像、JavaScript、CSSなど必要なファイルを提供する必要があります。Djangoでは、これらのファイルを "静的 (static) ファイル" と呼びます。 小さなプロジェクトではこのことは大きな問題になりません。 ウェブサーバが見つけられる場所で静的ファイ ... なる場合は、各アプリケーションが 持っている静的ファイルの集まりを複数扱うことになり、ややこしくなってきます。 django.contrib.staticfiles はまさにそのためにあります。これは静的なファイルを各アプリケーションから (さらに指定した別の場所からも) 一つの場所に集 ... Getting Help セクションに進んでください。 アプリ のルック & フィールをカスタマイズする 最初に、 polls ディレクトリの中に、 static ディレクトリを作成します。Django はそこから静的ファイルを探します。Django が polls/templates/ からテンプレートを探す方法と ... ... イスとして設定することができます。 この例では、サイトのルートには Django を設定していますが、 robots.txt 、 favicon.ico 、 /static/ 、そして /media/ の URL 空間は静的ファイルとして配信しています。他のすべての URL は mod_wsgi を使用して配信されます。 Ali ... as /robots.txt /path/to/mysite.com/static/robots.txt Alias /favicon.ico /path/to/mysite.com/static/favicon.ico Alias /media/ /path/to/mysite.com/media/ Alias /stati ... c/ /path/to/mysite.com/static/ <Directory /path/to/mysite.com/static > Require all granted </Directory> <Directory /path/to/mysite.com/media > Require a ... ... 次の2ステップからなります。まず、静的ファイルが変更されたときに collectstatic コマンドを実行します。その後、収集された静的ファイルのディレクトリ ( STATIC_ROOT ) を静的ファイルサーバーに移動し、配信でき ... るようにします。 staticfiles の STORAGES エイリアスに応じて、ファイルを手動で新しい場所に移動しなければならないこともありますが、 Storage クラスの ... 更する必要があります。以下に、役立つかもしれないいくつかの一般的なパターンを示します。 Having your Django application serve static files While web servers are much better and faster at serving static files, it is possible to have Django serve them using ... ... cated strings end with a translatable ellipsis sequence ("..."). See the documentation for truncatechars for more details. static template tag The staticfiles contrib app has a new static template tag to refer to files saved with the STATICFILES_STORAG ... e storage backend's url method and therefore supports advanced features such as serving files from a cloud service . CachedStaticFilesStorage storage backend The staticfiles contrib app now has a django.contrib.staticfiles.storage.CachedStaticFilesStor ... age backend that caches the files it saves (when running the collectstatic management command) by appending the MD5 hash of the file's content to the filename. For example, the file css/styles.css ... ... writing class-based views . Built-in support for using Python's logging facilities . Contrib support for easy handling of static files . Django's testing framework now supports (and ships with a copy of) the unittest2 library . Wherever possible, new ... rror are now handled as a logging activity. See the documentation on Django's logging interface for more details. Extended static files handling Django 1.3 ships with a new contrib app -- django.contrib.staticfiles -- to help developers handle the stat ... S, JavaScript, etc.) that are needed to render a complete web page. In previous versions of Django, it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL . Part of the purpose of introducing ... ... しい」表現にする上で便利な Django テンプレートフィルタのセットです。くわしくは django.contrib.humanize を参照してください。 static static STATIC_ROOT に保存されている静的ファイルにリンクするために、 Django には static テンプレートタグが同梱されています。 ... django.contrib.staticfiles アプリケーションがインストールされていれば、このタグは STORAGES で staticfiles で指定されたストレージの url() メソッド ... を使ってファイルを配信します。たとえば以下のようにします: {% load static %} < img src = " {% static 'images/hi.jpg' %} " alt = "Hi!" > 普通のコンテキスト変数を使うこともできます。例えば user_styl ... ... ー名です。例については 独自のパスコンバータを登録する を参照してください。 URLconf で使用するための django.conf.urls の関数 static() static. static ( prefix , view = django.views.static.serve , ** kwargs ) デバッグモードでファイルを配信するための URL パ ... ターンを返すヘルパー関数です: 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_ro ... 2026年10月08日