caldercho
caldercho

Reputation: 173

Django on Heroku - Broken Admin Static Files

I have a Django app running on Heroku/Cedar, configured as per the instructions at https://devcenter.heroku.com/articles/django

Using gunicorn as per Heroku's instructions fails to include the static files required for Django's admin to function. I can change the Procfile to "manage.py run_gunicorn" for local development, but that doesn't fly on Heroku.

I've searched all over for a fix - is there some way to include the admin static files without throwing them on S3 with my other static files?

Upvotes: 17

Views: 11577

Answers (9)

valem
valem

Reputation: 1870

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.

Upvotes: 2

yask
yask

Reputation: 4278

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.

Upvotes: 0

Kshitij
Kshitij

Reputation: 639

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.

Upvotes: 6

CodeTarsier
CodeTarsier

Reputation: 59

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.

Upvotes: 3

Stqs
Stqs

Reputation: 385

'django.contrib.staticfiles.views.serve'

instead of

'django.views.static.serve'

Upvotes: 0

Raveen Beemsingh
Raveen Beemsingh

Reputation: 554

just add these instead

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

using django 1.4.1

Upvotes: 8

Hacking Life
Hacking Life

Reputation: 3373

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,
    }),
 )

Upvotes: 8

Daniel  Magnusson
Daniel Magnusson

Reputation: 9674

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

Upvotes: 1

cberner
cberner

Reputation: 3040

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

Upvotes: 8

Related Questions