2012-05-05 55 views
13

我對Django(1.4)比較陌生,而且我很難理解靜態,媒體和管理文件背後的原理。該項目的結構不同,從一個教程到另一個,以及Webfaction(我將託管我的應用程序)的同一件事情。我想知道什麼是組織它的最佳方式,並且在將它部署到Webfaction時最少的痛苦和編輯,靜態媒體和adn管理文件有什麼意義? 預先感謝Django管理員,靜態和媒體文件混淆

回答

14

本質上你想在開發中由django提供靜態文件。一旦你準備投入生產,你希望服務器爲你做這件事(他們正在快速構建:-))

這是一個基本的設置,一旦你登錄服務器,你運行collectstatic命令獲得靜態根文件夾中所有staticfiles,你的服務器指向(參見重寫規則)

./manage.py collectstatic 

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 admin使用的靜態文件,django測試服務器只會找到它們,但在製作時,您必須複製或鏈接到這個文件夾讓管理員實際工作。

希望它可以幫助你看到事情變得更好......

+0

難道我不能只創建一個文件夾,我把我的網頁的所有CSS和JavaScript代碼,而不是將它們分開放在文件夾中?我的意思是,我已經準備好了網站佈局,但我只需要讓它與Django一起工作即可。我應該分開文件嗎? –

+0

您必須將靜態文件放在與python代碼或模板不同的目錄中,因爲您不想公開您的代碼,但除此之外,您可以按照自己的方式進行佈局。 您應該閱讀:https://docs.djangoproject.com/en/1.4/howto/static-files/ –

+0

瞭解filefinder在django文檔中的工作原理,這裏有很好的解釋,我在下面貼出了一個例子應該是有道理的,django將遵循你在文件搜尋器元組中使用的層次結構,並將所有這些文件收集到靜態根文件夾中,但在開發項目中,保持感覺分離到他們所屬的位置非常合適。 –

1

我的配置是:

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

和我的網站目錄是這樣的:

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