'Django: can't load CSS files that are in /static

Since I finished the backend of my Django website I started adding some style. My html files are displayed but it seems no css has been linked to them however the path is correct when I show the source code in Firefox.

mywebsite/
----blog/
----mywebsite/
----static/
--------css/
------------struct.css
----templates/
--------layouts/
--------errors/
------------404.html
--------html/
------------struct.html

Django version: 2.1.7

settings.py:

DEBUG = False
ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    # other apps...
    'django.contrib.staticfiles',
]

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

urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

struct.html:

{% load static %}

<!DOCTYPE html>
<html lang="en" dir="ltr">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width; initial-scale=1.0;">

    <link rel="stylesheet" href="{% static 'css/struct.css' %}">

    <title>test</title>

  </head>

  <body>

    <p>test</p>


  </body>

</html>

struct.css:

p{
    color:red:
}
body{
    background-color:black:
}


Solution 1:[1]

Generally static files path should be like this appname/static/appname/yourfiles there is no need to change url patterns.

Assuming your appname is Myapp , the proper path for your css file is

Myapp/static/Myapp/css/struct.css

For including static files , add below line in settings.py

STATIC_URL = '/static/'

and in your html template

<!doctype html>
{% load static %}
<link rel="stylesheet" href="{% static 'Myapp/css/struct.css' %}">

If you are having your static files within the app then there is no need to use

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

You can refer Managing Static Files in django documentation .

Solution 2:[2]

define static root in settings.py

STATIC_ROOT = BASE_DIR + '/static/'

hope it helps

refer this

Solution 3:[3]

if you are running this application on localhost then you may try to set DEBUG = True if you mistakenly set it to False, Because it was fixed for me that way.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 HeadShot191