J3punen
J3punen

Reputation: 13

Django - Failed to load resource: the server responded with a status of 404 (Not Found)

I'm trying to deploy a Django app that works locally, but doesn't on my website. The Django index.html (template) is shown but the errors below are shown and no css / js is loaded.

Failed to load resource: the server responded with a status of 404 (Not Found) - http://example.com/static/js/main
Failed to load resource: the server responded with a status of 404 (Not Found) - http://example.com/static/css/style.css

Lines I think are relevant in settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'src.pages'
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'pages/static'),
]

How I include the files in index.html template

{% load static %}
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
    </head>
    <body>
        <script src="{% static 'js/main.js' %}"></script>
    </body>
</html>

App structure is as follows

web_app/
├── src/
│   ├── config/
│   │   └── settings.py
│   ├── pages/
│   │   ├── static/
│   │   │   ├── css/
│   │   │   │   └── style.css
│   │   │   ├── js/
│   │   │   │   └── main.js
│   │   │   └── fonts/
│   │   ├── templates/
│   │   │   └── pages/
│   │   │       └── index.html
│   │   ├── urls.py
│   │   └── views.py
│   └── static (generated with collectstatic)/
│       ├── admin
│       ├── css/
│       │   └── style.css
│       ├── js/
│       │   └── main.js
│       └── fonts
├── manage.py
└── requirements.txt

Tried moving the JS code into index.html and it worked, so I'm guessing it can't find the static files for some reason.

I also tried setting the absolute path to the css file but it gives the same error.

Upvotes: 1

Views: 1272

Answers (1)

Mohamed ElKalioby
Mohamed ElKalioby

Reputation: 2334

Django won't serve static files, only the dev server (runserver) is configured to do so, you either let the static files to be serve from the HTTP server that in front of Django (e.g Apache2 or Nginx) which is recommended method, or use whitenouse

Upvotes: 1

Related Questions