Django - Static file not found

155,529

Solution 1

I confused STATIC_ROOT and STATICFILES_DIRS

Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.

STATICFILES_DIRS is the one that I need.

Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:

#STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
  os.path.join(SITE_ROOT, 'static/'),
)

Also don't forget to from django.conf import settings

Solution 2

There could be only two things in settings.py which causes problems for you.

1) STATIC_URL = '/static/'

2)

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

and your static files should lie under static directory which is in same directory as project's settings file.

Even then if your static files are not loading then reason is , you might have kept

DEBUG = False

change it to True (strictly for development only). In production just change STATICFILES_DIRS to whatever path where static files resides.

Solution 3

Serving static files can be achieved in several ways; here are my notes to self:

  • add a static/my_app/ directory to my_app (see the note about namespacing below)
  • define a new top level directory and add that to STATICFILES_DIRS in settings.py (note that The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting)

I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file admin-custom.css to override a couple of admin styles, I have a setup like so:

.
├── my_app/
│   ├── static/
│   │   └── my_app/
│   │       └── admin-custom.css
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static/
├── templates/
│   └── admin/
│       └── base.html
└── manage.py
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

This is then used in the template like so:

# /templates/admin/base.html
{% extends "admin/base.html" %}
{% load static %}

{% block extrahead %}
    <link rel="stylesheet" href="{% static "my_app/admin-custom.css" %}">
{% endblock %}

During development, if you use django.contrib.staticfiles [ed: installed by default], this will be done automatically by runserver when DEBUG is set to True [...]

https://docs.djangoproject.com/en/1.10/howto/static-files/

When deploying, I run collectstatic and serve static files with nginx.


The docs which cleared up all the confusion for me:

STATIC_ROOT

The absolute path to the directory where collectstatic will collect static files for deployment.

...it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).

https://docs.djangoproject.com/en/1.10/ref/settings/#static-root


Static file namespacing

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

https://docs.djangoproject.com/en/1.10/howto/static-files/


STATICFILES_DIRS

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

https://docs.djangoproject.com/en/1.10/howto/static-files/

Solution 4

If your static URL is correct but still:

Not found: /static/css/main.css

Perhaps your WSGI problem.

➡ Config WSGI serves both development env and production env

==========================project/project/wsgi.py==========================

import os
from django.conf import settings
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.wsgi import get_wsgi_application

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

Solution 5

  1. You can remove the STATIC_ROOT line
  2. Or you can create another static folder in different directory. For suppose the directory is: project\static Now update:
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'project/static/')
    ]
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Whatever you do the main point is STATICFILES_DIRS and STATIC_ROOT should not contain same directory.

I know it's been a long time but hope the new buddies can get help from it

Share:
155,529

Related videos on Youtube

Pierre de LESPINAY
Author by

Pierre de LESPINAY

Web developer at Masao. Fan of Django/python, Symfony/php, Vue/javascript, Docker EPITECH promo 2004.

Updated on July 08, 2022

Comments

  • Pierre de LESPINAY
    Pierre de LESPINAY almost 2 years

    I've seen several posts for this issue but didn't found my solution.

    I'm trying to serve static files within my Django 1.3 development environment.

    Here are my settings

    ...
    STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
      '/static/',
    )
    ...
    

    My urls.py

    urlpatterns = patterns('',
    ...
      url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root', settings.STATIC_ROOT}
      ),
    ...
    );
    

    My /home/glide/Documents/django/cbox/static/ directory is like

    css
      main.css
    javascript
    image
    

    I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.

    Do I have to specify patterns for css, javascript and images individually ?

    • Brendan Metcalfe
      Brendan Metcalfe about 3 years
      One tip people might find helpful - Django will not serve static content (i.e. CSS changes) when Debug is False
  • Pierre de LESPINAY
    Pierre de LESPINAY over 12 years
    No the urls.py should be like in the question (same thing for media) and don't forget to from django.conf import settings of course
  • chip
    chip over 6 years
    I just want to add to this answer, don't miss the s. it should be STATICFILES_DIRS. I did STATICFILE_DIRS, made me crazy for 45 minutes looking for the error.
  • Maduka Jayalath
    Maduka Jayalath almost 5 years
    I use BASE_DIR built-in const instead of SITE_ROOT and it works.
  • Smit Patel
    Smit Patel over 4 years
    In my case it was my environment variable for debug was false. I set it to true and now its working
  • Mukul_Vashistha
    Mukul_Vashistha almost 4 years
    Hi..I tried all the mentioned solutions but it still gives me 404. Can someone help with it please ?
  • SeanDp32
    SeanDp32 over 3 years
    this fixed it for me but I think I might need a better solution each app should have its own static folder in my case
  • Andreas L.
    Andreas L. over 3 years
    Using the my_app/static/my_app - approach, adding the line STATIC_ROOT = os.path.join(BASE_DIR, 'static') in the settings.py made the difference for me. Now, the files are found correctly. My use-case was binding my CV to my personal portfolio webpage like so (in the home.html): <a href="{% static 'portfolio/CV_Andreas_Luckert.pdf' %}">CV</a> where "portfolio" == my_app
  • Karthik Sunil
    Karthik Sunil about 3 years
    For me this I was using WSGI and publishing using gunicorn. This helped me.. Thanks
  • naheliegend
    naheliegend over 2 years
    @Naren Yellavula Why is it working for DEBUG = True?
  • squarespiral
    squarespiral almost 2 years
    For django 4 the settings are calles STATIC_DIRS and STATICFILES_DIRS. The latter is the one pointing to the static files for development - it is not in settings.py by default.