2012-05-30 43 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:错误。

相关问题