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?
You should store them externally on a service like S3 - while Heroku can serve static files, it's not designed to.
To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image. Just as with static files, in the production, you should always use a real web server to serve media files.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With