2012-05-05 12 views
13

私は比較的新しいDjango(1.4)を使用しています。静的、メディア、管理ファイルの背後にある理念を理解するのは苦労しています。プロジェクトの構造はチュートリアルごとに異なり、Webfaction(アプリケーションをホストする場所)も同じです。私はWebfactionにそれを配備しながら、それを整理して最小限の苦痛と編集で最適な方法が何であるか知りたいです。静的メディアadn管理ファイルのポイントは何ですか? ありがとうございますDjangoの混乱管理者、静的、メディアファイル

答えて

14

本質的には、開発中のdjangoによって静的ファイルを提供したいと思っています。プロダクションに入る準備が整ったら、サーバーはあなたにこれを実行させます(これはすぐに実行できるようにビルドされています:-))

ここでは、基本的な設定です。

./manage.py collectstatic 

(書き換えルールを参照)へのスタティックルートフォルダ内のすべてのstaticfilesを取得し、そのサーバーのポイントsettings.py

from os import path 
    import socket 

    PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in 

    # Dynamic content is saved to here 
    MEDIA_ROOT = path.join(PROJECT_ROOT,'media') 
    # if ".webfaction.com" in socket.gethostname(): 
    # MEDIA_URL = 'http://(dev.)yourdomain.com/media/' 
    # else: 
     MEDIA_URL = '/media/' 

    # Static content is saved to here -- 
    STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development 
    STATIC_URL = "/static/" 
    STATICFILES_DIRS = (
     ('', path.join(PROJECT_ROOT,'static')), #store site-specific media here. 
    ) 

    # List of finder classes that know how to find static files in 
    # various locations. 
    STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder', 
     'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
    ) 

settings_deployment.py

from settings import * 

DEBUG = False 
TEMPLATE_DEBUG = DEBUG 
MEDIA_URL = "http://yourdomain.com/media/" 

urls.py

...other url patterns... 

if settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() #this serves static files and media files. 
    #in case media is not served correctly 
    urlpatterns += patterns('', 
     url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 
      'document_root': settings.MEDIA_ROOT, 
      }), 
    ) 

django.conf(lighttpdの、これはapacheやnginxのかもしれない)が、私はwebfactionはこれを設定するアプリのサービスを持っていると信じて簡単に

$HTTP["host"] =~ "(^|\.)yourdomain\.com$" { 
    fastcgi.server = (
     "/django.fcgi" => (
      "main" => (
       "socket" => env.HOME + "/project/project.sock", 
       "check-local" => "disable", 
      ) 
     ), 
    ) 
    alias.url = (
     "/media" => env.HOME + "/project/media", 
     "/static" => env.HOME + "/project/static-root", 
    ) 

    url.rewrite-once = (
     "^(/media.*)$" => "$1", 
     "^(/static.*)$" => "$1", 
     "^/favicon\.ico$" => "/static/img/favicon.png", 
     "^(/.*)$" => "/django.fcgi$1", 
    ) 
} 
+2

STATICFILES_DIRS項目の末尾のカンマを誤って忘れてしまった場合、collectstaticは静的フォルダの内容ではなくすべてのコピーを開始します。私はそれに言及すると思ったので、私はしばらく時間がかかりました。 – Soviut

4

静的なファイルですカスタムJSスクリプト、アイコン、アプレットなどのような変更を加えずにサーバーが処理できるアプリケーションに必要なファイルです。これを使用する最も良い方法は、アプリケーションの各フォルダの静的フォルダに静的ファイルを配置することです。このように、テストサーバーはそこに見つけて、プロダクションサーバーにデプロイする場合は、python manage.py collectstaticを実行して、settings.pyで定義されたルート静的フォルダにすべてコピーする必要があります。

メディアファイルはアバターの写真など、あなたのアプリケーションのユーザーによってアップロードされたもの。

管理者ファイルはDjango管理者が使用する静的ファイルです.Djangoテストサーバーはそれらを検索しますが、実稼働環境ではコピーまたはリンクする必要がありますこのフォルダは管理者が実際に動作するためのものです。

1.settings.py

# Absolute filesystem path to the directory that will hold user-uploaded files. 
    # Example: "/var/www/example.com/media/" 
    MEDIA_ROOT='/media/' 

    # URL that handles the media served from MEDIA_ROOT. Make sure to use a 
    # trailing slash. 
    # Examples: "http://example.com/media/", "http://media.example.com/" 
    MEDIA_URL = '/media/' 

    # Absolute path to the directory static files should be collected to. 
    # Don't put anything in this directory yourself; store your static files 
    # in apps' "static/" subdirectories and in STATICFILES_DIRS. 
    # Example: "/var/www/example.com/static/" 
    STATIC_ROOT = '/static/' 

    # URL prefix for static files. 
    # Example: "http://example.com/static/", "http://static.example.com/" 
    STATIC_URL = '/static/' 


    # Additional locations of static files 
    STATICFILES_DIRS = (
     '/'.join(__file__.split(os.sep)[0:-2]+['static']), 
     # Put strings here, like "/home/html/static" or "C:/www/django/static". 
     # Always use forward slashes, even on Windows. 
     # Don't forget to use absolute paths, not relative paths. 
    ) 

    # List of finder classes that know how to find static files in 
    # various locations. 
    STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder', 
     'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
    ) 

2.urls.py

from django.conf import settings 
    if settings.DEBUG: 
     from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
     urlpatterns += staticfiles_urlpatterns() 

+0

フォルダに分割するのではなく、WebページのCSSとJavaScriptのコードをすべて1つのフォルダに作成できますか?つまり、私はサイトのレイアウトを準備しているが、私はちょうどそれがDjangoで動作するようにする必要があります。ファイルを分割する必要がありますか? –

+0

コードを公開するのではなく、必要なレイアウトにレイアウトすることができないため、静的ファイルをPythonコードまたはテンプレートとは別のディレクトリに配置する必要があります。 https://docs.djangoproject.com/en/1.4/howto/static-files/ –

+0

ファイルファインダーがdjangoのドキュメントでどのように機能するのかを読んでください。そこに詳しい説明があります。 djangoはファイルファインダータプルで使用する階層に従い、静的ルートフォルダにこれらのファイルをすべて集めますが、devプロジェクトでは、それらが所属する場所に感覚を分けておくのが理にかなっています。 –

1

私の設定がある...それはあなたがより良いものを見ることができますホープ私のサイトディレクトリは次のようなものです:

root 
    │ manage.py 
    │ 
    ├─media 
    ├─my_django_py3 
    │ settings.py 
    │ urls.py 
    │ views.py 
    │ wsgi.py 
    │ __init__.py 
    │ 
    ├─static 
    │  9gq05.jpg 
    │  ajax.js 
    │  favicon.gif 
    │ 
    ├─templates 
    └─utils 
関連する問題