[Fixed]-Django on Heroku – Broken Admin Static Files

8👍

If you use runserver and configure your app with DEBUG=True, then it will serve the admin files just like on your development machine. However, this is definitely not the recommended way to do it, and I would suggest that you put them on S3.

Using the django-storages app it’s very easy to configure collectstatic to automatically push all the admin files to S3. You can find directions here

8👍

Check out this post: http://matthewphiong.com/managing-django-static-files-on-heroku

If that doesn’t work for you try adding the following to your urls.py after the normal url pattern tuple. Make sure you have your STATIC_ROOT set and you’ve run collect static on your local environment all before pushing to heroku.

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

8👍

just add these instead

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

using django 1.4.1

6👍

It seems little late compared to the asked date. But I got into this issue and spent 30 mins on what I did wrong. So here it is the magic solution for those who might fall in this trap.

There is some problem with Heroku’s django.contrib.staticfiles.urls

SOLUTION

You need to install dj-static (Link to download) on your Heroku setup.
It’s a Django middleware utility that allows to properly serve static assets from production with a WSGI server like Gunicorn.

I hope this will help someone.

3👍

create ‘static’ folder into your ‘project_directory’.

set the ‘STATIC_ROOT’ path in ‘settings.py’ file which can serve your admin-site’s static files.

STATIC_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'static'))

Add STATIC_ROOT in ‘/urls.py’

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

Run the following command that will copy all the admin static files into project’s static folder.

python manage.py collectstatic

Now do git add, commit and push heroku master.

2👍

If you are deploying to Heroku without using whitenoise (which I would suggest), definitely use dj_static https://pypi.python.org/pypi/dj-static!

I spent the past 3 hours trying to serve my files to heroku and dj_static worked within a matter of 2 minutes.

👤valem

1👍

I got django admin working with following edits

urls.py(at the end)

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

Procfile

web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT

0👍

‘django.contrib.staticfiles.views.serve’

instead of

‘django.views.static.serve’

👤Stqs

0👍

Follow this to fix all static related issues with Django and heroku.

In your settings.py paste this at the end

import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
    )
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    )

STATIC_URL = '/static/'

Your template for a particular app should be in app_name/templates/app_name/

When you render template this is how you will specify template name

in views.py

.....
return render(request,'app_name/template_name.html',context)

For static files place your files here:

project_folder/app_name/static/app_name/css

project_folder/app_name/static/app_name/js

project_folder/app_name/static/app_name/img

to access your static file use path app_name/css/style_name.css

If you follow this, all your static files will load up fine in heroku as well as in your local development machine.

👤yask

Leave a comment