1

Same question as this 7 year old one but the solutions don't help plus I'm using django 3 so in my opinion clearly not a duplicate.

I followed exactly the Django documentation about static files.

settings file:

STATIC_URL = '/static/'
DEBUG = True

Folder Structure:

---my_project
------my_project
------app1
------static
---------css
------------mystyle.css

Template:

  {% load static %}
  <link rel="stylesheet" href="{% static "css/mystle.css" %}">

When browsing to the site I get a 404 Not Found. The link is pointing to the correct directory:

http://127.0.0.1:8000/static/css/mystyle.css

With further search and looking at the documentation (which in my opinion is unclear) I also found the setting STATIC_ROOT and set it accordingly but that did not help either.

#BASE_DIR = path to project dir
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

What am I doing wrong?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
beginner_
  • 7,230
  • 18
  • 70
  • 127
  • have you added urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) – bmons Mar 26 '20 at 08:12
  • see my answer. I just figured it out. In my opinion the documentation is confusing and doesn't show a nice, full example of how the setting should be. – beginner_ Mar 26 '20 at 08:24
  • Funny, the same typo as here https://stackoverflow.com/questions/60859511/django-doesnt-load-css-file/60861806#60861806 – Ivan Starostin Mar 26 '20 at 08:41

1 Answers1

1

Trying to solve this for hours and then when you hit send on the post you find the solution. For me the django documentation is unclear and confusing hence I'm adding an answer instead of deleting my question.

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.

With emphasis on "can" and "also". This implies this setting isn't needed and STATIC_URL is enough. But it isn't. It's aboslutley required or else you get the 404.

This leads to below settings:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
DEBUG = True

to make it work.

beginner_
  • 7,230
  • 18
  • 70
  • 127
  • STATICFILES_DIRS is described correctly in the docs: it is an optional additional list of directories to be searched and collected info STATIC_ROOT. In shown conf you don't have STATIC_ROOT defined which is a mistake. In your case you need STATIC_ROOT only. STATIC_URL is not a folder - it is an root URL under which your files can be queried via web. – Ivan Starostin Mar 26 '20 at 08:36
  • With STATIC_ROOT only it doesn't work. In fact the documentation I linked doesn't mention it for development, only under deployment. The documentation might work for someone familiar with django but since I'm new to django, it's very confusing and there is not example shown with all settings that actually work. If I omit `STATICFILES_DIRS` it simply doesn't work. – beginner_ Mar 26 '20 at 08:48
  • Then you have error somewhere else. STATICFILES_DIRS is absolutely not required for static files to work. – Ivan Starostin Mar 26 '20 at 08:57
  • Thanks. I had the same issue. Only after including "STATICFILES_DIRS" did it work. – RunLoop Jul 18 '22 at 05:01