Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

Heroku - Handling static files in Django app

I have a project (myapp) in heroku but I can't get the static files to work properly. I was following this blog post.

My Procfile looks like this:

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

settings.py:

...

STATIC_ROOT = os.path.join(PROJECT_PATH, 'staticfiles')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    # I have the static folder inside my app and not inside the project
    os.path.join(PROJECT_PATH, 'cesar/static'),
)

...

When restarting using heroku restart this is what the heroku logs shows:

...
Copying ...

114 static files copied to '/app/myapp/staticfiles'.
...

But when I do heroku run ls -l myapp/ I can't see the staticfiles folder:

-rw------- 1 u5605 5605    0 Jan 28 16:53 __init__.py
drwx------ 4 u5605 5605 4096 Jan 28 16:53 cesar
-rw------- 1 u5605 5605  503 Jan 28 16:53 manage.py
-rw------- 1 u5605 5605 6292 Jan 28 16:53 settings.py
drwx------ 2 u5605 5605 4096 Jan 28 16:53 templates
-rw------- 1 u5605 5605  257 Jan 28 16:53 urls.py
-rw------- 1 u5605 5605  286 Jan 28 16:53 views.py

What am I missing or doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a solution. This was my initial myapp/urls.py:

from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.conf import settings


admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('myapp.cesar.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I added these lines to the end of the original myapp/urls.py file:

if not settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

Now it's working fine. I hope this helps someone else too


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...