djangodjango-admincloud-foundrydjango-static

Django project on Cloud Foundry not loading admin interface CSS


I have a simple Django project deployed with Cloud Foundry. The admin interface (the only interface used by my simple project) looks fine when running locally. When deployed, however, the CSS is visibly broken. The URLs for the CSS and JS assets return 404.

Other answers to similar questions say that the solution is the collectstatic command. To run this in the context of Cloud Foundry, I type the following (obviously with the real app name and path):

cf run-task app-name -c "python app-path/manage.py collectstatic"

The output logged from this looks very promising:

128 static files copied to '/home/...path.../settings/static'.

Yet, in spite of this apparent promise, the CSS is still broken.

FYI, my settings include this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

STATIC_URL = '/static/'

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

Solution

  • Answering my own question: what I was trying to do simply does not work. Django's runserver command only serves static files if DEBUG = True is in the appropriate settings file, or if the --insecure switch is used. Both of these are considered inappropriate for deployment use.

    Although the staticfiles documentation claims the app organizes static files "that can easily be served in production" it's not really so easy. Ideally, the documentation should clarify that collectstatic only makes files available if the web server is configured to expect them and serve them from wherever the command puts them.

    Here are my potential paths forward as I understand them:

    1. Use a web server like nginx, Gunicorn, or Apache
    2. Serve static assets from a cloud provider like AWS S3
    3. Simply run collectstatic --insecure which, obviously, is considered insecure