我是新來的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()
錯誤是不是在我的短信..是的,這是真的很難幫助我。 (對不起)我覺得很愚蠢^^'
現在我使用這種方法進行一些修改,如我的編輯中所述。 (因爲我的靜態目錄('靜態/')在我的應用程序dir('blogapp /'))中。它解決了我的問題(即使現在我與另一個-_-) – heathen90
這個設置文件的父目錄中是否有'blogapp /'? 「blogapp」似乎更可能與設置文件位於同一目錄中,這意味着您應該只加入'blogapp /'而不是'../ blogapp'。 –
appheroku /包含monblog /目錄,blogapp /目錄和staticfiles目錄。 blogapp /目錄包含models.py文件,views.py文件,admin.py,..(etc)和static/dir。 monblog /目錄包含settings.py,urls.py等... – heathen90