Search code examples
djangoapachedjango-staticfilesstatic-files

Static Files not being found


I'm having issues with serving static files to my production server. I keep getting "WARNING:django.request:Not Found: /static/admin/css/base.css" along with the other static files within my terminal upon loading the page at my domain.

Here's some of my code in my settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django_extensions',
    'sslserver',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'axes',
    'rest_framework',
]

...

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

And here's my code within my httpd.conf and httpd-ssl.conf in my Apache files.

Alias /static/ "C:/Users/MyUser/Desktop/MyProject/static/"

<Directory "C:/Users/MyUser/Desktop/MyProject/static">
    Require all granted
</Directory>

and in my httpd-ssl.conf:

Alias /static/ "C:/Users/MyUser/Desktop/MyProject/static/"

<Directory "C:/Users/MyUser/Desktop/MyProject/static">
    Require all granted
</Directory>

<Directory "C:/Users/MyUser/Desktop/MyProject/">
    Options Indexes FollowSymLinks
    Require all granted
</Directory>

I have my DEBUG = False

I am also using Apache and Waitress on a Windows VM. Im running my server via: waitress-serve --listen=127.0.0.1:8000 MyProject.wsgi:application

Here's my project directory:

MyProject/
├── manage.py
├── db.sqlite3
├── static/
│   └── ... (my static files)
├── staticfiles/
│   └── ... (collected static files)
└── MyProject2/
    ├── settings.py
    ├── urls.py
    ├── wsgi.py
    └── __init__.py

also heres my wsgi.py:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HaltScanner.settings')
if settings.DEBUG:
    application = StaticFilesHandler(get_wsgi_application())
else:
    application = get_wsgi_application()

# Add the HaltProject directory to PYTHONPATH
sys.path.append(r'C:\Users\MyUser\Desktop\MyProject')

# Debugging prints
print("PYTHONPATH:", sys.path)
print("sys.path:", sys.path)
print("os.environ:", os.environ)
print("Current Working Directory:", os.getcwd())



# Debugging print for settings
print(settings)

I have not tried much outside of this since I don't know what else I can do.


Solution

  • Since you have DEBUG = False, Django will not handle static files. Here are several solutions you can try.

    Inside your MyProject/MyProject2/urls.py file, you can add another urlpattern to look for static files under the url /static/*:

    from django.views.static import serve 
    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        ...,
        url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT})
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    If you get mimetype errors of anytype, like I have had before in the past, you can import mimetypes in your MyProject/MyProject2/settings.py file:

    import mimetypes 
    mimetypes.add_type("text/css", ".css", True)
    

    Also, if you still want to set DEBUG = False and serve static files locally for testing, you can runserver in insecure mode:

    python3 manage.py runserver --insecure