2012-05-30 42 views
16

我是新來的heroku,我嘗試了一個簡單的django應用程序沒有CSS。Django heroku靜態目錄

但我只是在我的應用程序添加一個CSS文件,當我做到這一點:

git push heroku master 

靜態文件收集失敗:

[...] 
-----> Collecting static files 
Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line 
    utility.execute() 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute 
    output = self.handle(*args, **options) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle 
    return self.handle_noargs(**options) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs 
    collected = self.collect() 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 104, in collect 
    for path, storage in finder.list(self.ignore_patterns): 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 105, in list 
    for path in utils.get_files(storage, ignore_patterns): 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 25, in get_files 
    directories, files = storage.listdir(location) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 235, in listdir 
    for entry in os.listdir(path): 
OSError: [Errno 2] No such file or directory: '/home/kevin/web/django/appheroku/blogapp/static' 
!  Heroku push rejected, failed to compile Python/django app 

To [email protected]:[...] 
! [remote rejected] master -> master (pre-receive hook declined) 
error: failed to push some refs to '[email protected]:[...]' 

這裏是我的settings.py:

[...] 
STATIC_ROOT = '/home/kevin/web/django/appheroku/staticfiles' 

STATIC_URL = '/static/' 

STATICFILES_DIRS = (
    '/home/kevin/web/django/appheroku/blogapp/static', 
) 
[...] 

該urls.py:

[...] 
if settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

Procfile:

web: python appheroku/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT appheroku/monblog/settings.py 

它似乎連一個 '/ home /凱文/網絡/ Django的/ appheroku/blogapp /靜態' 目錄中,未檢測到它,我想不通爲什麼。 :(

請幫我^^

編輯:

現在我的settings.py看起來是這樣的:

import os 
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../blogapp') 
[...] 
STATIC_ROOT = os.path.join(PROJECT_ROOT,'staticfiles/') 
STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static/'), 
) 
[...] 

而現在的靜態文件收集步驟似乎運作良好:

-----> Collecting static files 
     74 static files copied. 

還有一個問題: 在我的proc文件中,'appherok未找到u/manage.py','bin/gunicorn_django'和'appheroku/monblog/settings.py'。 我固定它,現在我procfile看起來是這樣的:

web: python manage.py collectstatic --noinput; gunicorn_django --workers=4 --bind=0.0.0.0:$PORT monblog.settings 

的應用程序不會崩潰了,但仍然沒有CSS。 這裏是日誌:

'2012-05-31T17:35:16+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=85ms status=200 bytes=1054 
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/css/style.css dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2088 
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/js/jquery-1.7.2.min.js dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2115' 

EDIT3:

是的!它的工作原理^^ 問題出在我的urls.py上。我寫道:

[...] 
if not settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

,而不是

[...] 
if settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

錯誤是不是在我的短信..是的,這是真的很難幫助我。 (對不起)我覺得很愚蠢^^'

回答

12

問題是您在Heroku服務器中找不到的絕對路徑爲STATIC_ROOT

要解決它,請考慮以下方法。

在您的settings.py:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/') 
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static/'), 
) 
+0

現在我使用這種方法進行一些修改,如我的編輯中所述。 (因爲我的靜態目錄('靜態/')在我的應用程序dir('blogapp /'))中。它解決了我的問題(即使現在我與另一個-_-) – heathen90

+0

這個設置文件的父目錄中是否有'blogapp /'? 「blogapp」似乎更可能與設置文件位於同一目錄中,這意味着您應該只加入'blogapp /'而不是'../ blogapp'。 –

+0

appheroku /包含monblog /目錄,blogapp /目錄和staticfiles目錄。 blogapp /目錄包含models.py文件,views.py文件,admin.py,..(etc)和static/dir。 monblog /目錄包含settings.py,urls.py等... – heathen90

3

你在路上。發生什麼情況是Heroku試圖使用您的計算機上本地存在的STATICFILES_DIRS設置(/ home/kevin/web/django/appheroku/blogapp/static),但在Heroku服務器上不存在。

一個簡單的解決方案是從staticfiles_dir變量刪除了這一行,所以你有:

STATICFILES_DIRS =() 

然後默認STATICFILES_FINDERS會踢,你要善於去在本地運行的應用程序,以及部署在Heroku上。

+0

謝謝,我現在明白了。您的解決方案使「沒有這樣的文件或目錄」錯誤消失,但現在有這樣的錯誤:「追溯(最近一次調用最後):OSError:[Errno 30]只讀文件系統:'/ home/kevin' – heathen90

+0

因此,新的錯誤應該由Kay Zhu的建議來解決,關於沒有顯示CSS,當你嘗試直接去一個css頁面時,heroku日誌中有什麼錯誤?你可以用「heroku logs - tail 「在命令行上更好地處理掛斷是什麼 – eliotk

+0

GET xxx-xxx-number.herokuapp.com/static/css/style.css dyno = web.1 queue = 0 wait = 0ms service = 6ms status = 404字節= 2088 – heathen90

1

對於任何人誰是未來的我一樣:

像@eliotk意味着,

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/') 
STATICFILES_DIRS =() 

這是要避免FileNotFoundError: [Errno 2] No such file or directory:的配置錯誤或OSError: [Errno 30] Read-only file system:錯誤。

相關問題