[Django]-Django website is giving 404 error for static files

4👍

There are 2 issues:

1. is the syntax error that Digitiain pointed out in his answer:

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

2. You cannot add the path in STATIC_ROOT to your STTATICFILES_DIRS. So if your STTATICFILES_DIRS looks like above, STATIC_ROOT cannot be os.path.join(BASE_DIR, "static").


The first point should be fairly clear. The second also confused me in the beginning. But it makes a lot of sense actually:

In STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to serve static files; this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them… – exactly! – in the STATIC_ROOT folder. Now it becomes clear why the STATIC_ROOT folder cannot really be part of STATICFILES_DIRS as this would mean django would need to put the folder into itself. Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that.

I hope that helps to clarify the relation between STATICFILES_DIRS and STATIC_ROOT. Bottom-line: Choose a different path for STATIC_ROOT and you should be good to go!


As an example, you could have a project structure like this:

base_dir/
  - static/
      - css/
      - js/
      - ...
  - static_collected/
  - ...

Then it would be valid to define:

# settings.py
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")
👤j-i-l

1👍

This might be a red herring, but in the Django docs the syntax for setting a path relative to your BASE_DIR includes os.path.join, so yours would look like:

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

EDIT: I also just noticed that you referenced DIR rather than DIRS, which could be causing grief. Ditto with templates.

Leave a comment