Forums

Django static files not working on my site.

I am well aware that the issue in the title is already a popular subject, however, I have not found a solution that has worked for me. I am trying to have my site serve static files, I have done all the steps that I know of.

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

This is my static definition in settings.py. I have executed the command collectstatic --noinput. The folder was generated.

In the web tab of PythonAnywhere, I put /static/ in the URL and /home/username/.virtualenvs/venv/Django/project/staticfiles.

My security settings in settings.py are:

if DEBUG:
    SECURE_BROWSER_XSS_FILTER = False
    SECURE_CONTENT_TYPE_NOSNIFF = False
    X_FRAME_OPTIONS = 'DENY'
    SECURE_SSL_REDIRECT = False
    SECURE_HSTS_SECONDS = 0
    SECURE_HSTS_PRELOAD = False
    SECURE_HSTS_INCLUDE_SUBDOMAINS = False
    SESSION_COOKIE_SECURE = False
    CSRF_COOKIE_SECURE = False
else:
    # Set stronger security settings for production
    SECURE_BROWSER_XSS_FILTER = True
    SECURE_CONTENT_TYPE_NOSNIFF = True
    X_FRAME_OPTIONS = 'DENY'
    SECURE_SSL_REDIRECT = True
    SECURE_HSTS_SECONDS = 86400
    SECURE_HSTS_PRELOAD = True
    SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True

As far as i understand, I have all the / in the right places and paths pointing to the right directory.

Source code points to /home/username/.virtualenvs/venv/Django/project

Any help is appreciated because I have tried Google, GPT, other forums posts and Youtube videos where it just works but for some reason, I am having trouble.

Does your code lives in virtual environment directory? It is not a normal way of setting stuff. Also, see https://help.pythonanywhere.com/pages/DjangoStaticFiles

I actually fixed it. It was in a virtual env but that was not the issue. My static folders were in all caps for example CSS and in my code, they were css.

Stupid mistake that I thought was not an issue because it worked locally fine. After changing css to CSS and images to IMAGES in my code, it worked like a charm.

Right -- file names in Linux are case sensitive! Glad you figured that out.