Django – CSS File not Found | Django Static Files Tutorial
In the Django CSS file not found, I confused STATIC_ROOT and STATICFILES_DIRS.
Add a static/your_app/ directory to your_app define a new top-level directory and add that to STATICFILES_DIRS in settings.py.
The solution for me is to not use STATIC_ROOT and set my common files directory in STATICFILES_DIRS :
├──crudexample --> project root
├──crudexample --> Django root
├──asgi.py
├──__init__.py
├──__init__.pyc
├──__pycache__
├──settings.py
├──settings.pyc
├──urls.py
├──urls.pyc
├──wsgi.py
├──db.sqlite3
├──log.log
├──manage.py
├──students
├──admin.py
├──admin.pyc
├──apps.py
├──forms.py
├──__init__.py
├──__init__.pyc
├──migrations
├──models.py
├──models.pyc
├──__pycache__
├──static
├──css
├──bootstrap.min.css
├──js
├──bootstrap.min.js
├──jquery-3.4.1.min.js
├──templates
├──index.html
├──tests.py
├──urls.py
├──views.py
#settings.py
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
This is then used in the template like so :
# /templates/index.html
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
=== Or ===
<link rel="stylesheet" href="static/css/bootstrap.min.css">