0

I am trying to get a django web site into production. The dev version works fine, but in production the application does not seem to find my css files.

I know that there are a lot of queries similar to mine and I have perused many and tried the suggested solutions. The production version seems to work perfectly apart from the static files issue

In settings.py I have

DEBUG=False

and

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

I have run

python manage.py collectstatic

and the css and js directories are copied correctly into my_app/static_files

I have restarted the server but it still does not function correctly.

My tree on the production server is:

.
├── my_app
│   ├── __init__.py
│   ├── settings.py
│   ├── static
│   │   ├── css
│   │   │   ├── main.css
│   │   └── js
│   ├── templates
│   │   ├── base.html
│   ├── urls.py
│   └── wsgi.py
├── boards
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── boards
│   │       ├── about.html
│   │       ├── contact.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── passenger_wsgi.py
├── static_files
│   ├── css
│   │   ├── main.css
│   └── js
├── _templates
Psionman
  • 3,084
  • 1
  • 32
  • 65

2 Answers2

0

Web servers handle this. What are you using as a server? For example, on Nginx we have paths for both static/media files.

server {
       set $app_name 'APP_NAME';
       set $root 'ROOT/$app_name';

       listen 8000;
       server_name 127.0.0.1;
       location /static/ {
            root $root/;
       }

       location /media/ {
            root $root/attachments/;
       }

       location / {
            include proxy_params;
            proxy_pass http://unix:$root/$app_name.sock;
      }
}
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0

After hours of going through code line by line and inspired by an answer to this question I spotted this in wsgi.py (which didn't look right):

if settings.DEBUG:
    application = StaticFilesHandler(get_wsgi_application())
else:
    application = get_wsgi_application()

I have changed it to:

if settings.DEBUG:
    application = get_wsgi_application()
else:
    application = StaticFilesHandler(get_wsgi_application())

and now it finds the static files and works perfectly.

Psionman
  • 3,084
  • 1
  • 32
  • 65