2014-08-31 81 views
2

我用Django開發了一個Python應用程序,我只是將它部署到了Heroku服務器上。Heroku Django應用程序靜態文件沒有提供(錯誤OSError:[Errno 2])

我的settings.py文件有這樣的內容:

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__)) 
STATIC_ROOT = 'staticfiles' 
STATIC_URL = '/static/' 

STATICFILES_DIRS = (
     os.path.join(PROJECT_PATH, 'static'), 
) 


    STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    #'django.contrib.staticfiles.finders.DefaultStorageFinder', 
    ) 

當我運行這個命令:

heroku run python manage.py collectstatic --noinput 

我得到以下錯誤:

OSError: [Errno 2] No such file or directory: '/app/nameOfApp/static' 

在模板中的網址到以前存儲的文件,比如CSS和JavaScript文件,這些文件中都有這種URL模板文件:

<script type="text/javascript" src="{% static "javascripts/jquery.min.js" %}" ></script> 

適當服務,但那些通過管理面板上傳新的文件並不存儲在同一個地方作爲靜態文件夾中,即新上傳的文件應存放在同以前的文件文件夾作爲其他靜態文件,如CSS和javascripts,

問題: 所以我應該如何修改settings.py文件以使其正常工作?

+0

上傳的文件?媒體設置如何? – 2014-09-01 05:00:27

+0

@rajasimon MEDIA_ROOT和MEDIA_URL被留空,我應該怎麼做?順便說一句,他們也是空的,當我在本地運行代碼,但它曾用於本地工作 – Siavosh 2014-09-01 06:45:48

回答

2

媒體根目錄是用於保存用戶上傳文件的目錄的絕對文件系統路徑。

Example: "/var/www/example.com/media/" 

媒體URL用於處理從媒體根服務的媒體文件。另一個重要的不是you will need to configure these files to be served in both development and production

加入這一行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) 
+0

我的模板照片網址有像這樣的src =「/靜態/ {{image.image.url}}」和模型模型有屬性像這樣image = models.ImageField(upload_to =「images/subFolder」)MEDIA_ROOT和MEDIA_URL的內容應該是什麼? – Siavosh 2014-09-01 07:33:38

+0

MEDIA_URL ='/ media /'並將其設置爲MEDIA_ROOT = os.path.join(PACKAGE_ROOT,「site_media」) - – 2014-09-01 07:55:34