Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 Static file not found - Django

I have an issue with django. I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel). All worked fine with AWS but when I switched to Cpanel all statics files were missing.

this is my settings.py:

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    ]

my project structure:

my_project
|-app/
    |-...
    |-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
    |-...
    |-settings.py
|-static/
    |-main_page/
        |-js/
            |-my-script.js

I add static files like this:

{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>

This is the error:

GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)

When I go to the URL of the file it understands it like one of my URLs:

404 error screenshot

I hope you will help me to solve this issue ;) thanks.

like image 637
leo_sheecool Avatar asked Feb 03 '26 01:02

leo_sheecool


2 Answers

you need to add the static & media files config in the urls.py , like this

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] 
urlpatterns  +=  static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/

like image 198
Mahmoud Ahmed Avatar answered Feb 04 '26 15:02

Mahmoud Ahmed


For those who still looking for static file solution, please try Django package Whitenoise. It is easy to install and easy to use if you followed the instructions.

some simplified steps:

  1. Collect static - make sure static files existing before install

     python manage.py collectstatic
    
  2. Install the Whitenoise - this step depends how you managed the packages, update proper file(e.g. Pipfile or requirements.txt) and install. Below command just a example to install the package.

     pip install whitenoise
    
  3. Update the static root in settings.py

     STATIC_ROOT = BASE_DIR / "staticfiles"
    
  4. Add the following to your MIDDLEWARE in settings.py - from Whwitenoise docs, Whitenoise package should place after django.middleware.security.SecurityMiddleware

     `MIDDLEWARE = [
       'django.middleware.security.SecurityMiddleware',
       'whitenoise.middleware.WhiteNoiseMiddleware', #add it here exactly after security middleware
       ...
     ]
    
  5. now restart or rebuild the app to check whether it is working for you.

Please check Whitenoise docs if you running into issue about installing the Whitenoise(Whitenoise docs)

a message for using staticfiles_urlpatterns: this only works when DEBUG=True in settings.py which means you should NOT use it for production environment. see reference here

like image 30
Wei Jing Avatar answered Feb 04 '26 14:02

Wei Jing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!