Reputation: 7632
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?
Upvotes: 1
Views: 1078
Reputation: 7632
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.
Upvotes: 1