为什么项目无法推送到Heroku?

按教程将项目推送到heroku,前面步骤一切顺利没有任何错误,直到这里出现问题在网上没有找到解决方法。。。

感觉好像是setting.py的设置有问题,但和教程里对照是一样的:

heroku的设置

if os.getcwd()=='/app':

import dj_database_url
DATABASES={
    'default':dj_database_url.config(default='postgres://localhost')
}

SECURY_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO','https')

ALLOWED_HOSTS = ['*']

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

命令行的报错:

(ll_env) G:python_dorest>heroku create
Creating app... done, protected-crag-1803
https://protected-crag-1803.h... | https://git.heroku.com/protec...

(ll_env) G:python_dorest>git push heroku master
Counting objects: 48, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (39/39), done.
Writing objects: 100% (48/48), 11.18 KiB | 0 bytes/s, done.
Total 48 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.5.2

—————————中间内容省略—————————

remote: File "/app/.heroku/python/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 50, in path
remote: raise ImproperlyConfigured("You're using the staticfiles app "
remote: django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
remote:
remote: You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote: https://devcenter.heroku.com/...
remote: ! Push rejected, failed to compile Python app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to protected-crag-1803.
remote:
To https://git.heroku.com/protec...
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/protec...'

阅读 12k
8 个回答
新手上路,请多包涵

我也碰到了跟你一样的问题,弄了半天总算是没有报错了。
如果我没有猜错的话你应该也是在看Python从入门到实践这本书吧。

我解决的办法是在settings.py文件中加了一条:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
特别注意这一条不要加在Heroku设置的那个if语句当中!!!要加在外边。

希望能够帮你解决这个问题。

新手上路,请多包涵

You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1


显示可能是heroku服务器已经有一个静态文件占位了,所以要删掉它。
执行命令:heroku config:set DISABLE_COLLECTSTATIC=0

我也遇到了同样的问题,用这种方法解决了,希望能帮助你和后来者~

新手上路,请多包涵

静态资产配置

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_DIRS = (

os.path.join(BASE_DIR, 'static'),

)

这段顶格写就好了,《Python从入门到实践》排版错误,感谢二楼和这个项目源码github

好好看错误日志啊:

remote: File "/app/.heroku/python/lib/python3.5/site-packages/django/contrib/staticfiles/storage.py", line 50, in path
remote: raise ImproperlyConfigured("You're using the staticfiles app "
remote: django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

上面的三种方法都不行呀。最新的更改成0的,最后部署上去 显示的是applicaition error.
Checking for the /tmp directory
The if statement in settings.py looks like this in the book:

Heroku settings

if os.getcwd() == '/app':

import dj_database_url

Here’s the updated version, which checks for /tmp directories as well:

Heroku settings

cwd = os.getcwd()
if cwd == '/app' or cwd[:4] == '/tmp':

import dj_database_url

参考原作者的网址,里面有各种问题的回答
http://ehmatthes.github.io/pc...

这个问题已经一年多了,后来我用了阿里云。。。

新手上路,请多包涵

花了一个早上时间查看官方文档和论坛答案,最后还是在这里解决了问题,总结一下大家的方法,末尾贴上源码并总结一下:
1、if代码做如下处理
cwd = os.getcwd()
if cwd == '/app' or cwd[:4] == '/tmp':

2、os.path.join(BASE_DIR, 'static') 语句需定格

3、部署前执行命令:heroku config:set DISABLE_COLLECTSTATIC=0
清空服务器的占位静态文件

4、执行部署命令

源码

Heroku setting

cwd = os.getcwd()
if cwd == '/app' or cwd[:4] == '/tmp':

import dj_database_url
DATABASES = {
    'default': dj_database_url.config(default='postgres://localhost')
}
 # make request.is_secure() admit x-forwarded_Photo header
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PHOTO', 'http')
 
 # support all host header
ALLOWED_HOSTS = ['*']
 
 # static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题