Why is Django not loading my CSS?

Viewed 10491

I am creating a website with Django and for some reason my CSS file is having no effect on the page. I have checked to make sure my STATIC_URL is defined but still no luck.

My settings.py:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Inside of my blog app I have a static directory

blog
  |
  static
     |
     css
       |
       blog.css

My HTML doc:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Medicare Supplemental info</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <!-- This is where I'm loading the CSS file -->
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
    </head>

I checked to make sure that I have the required app installed in the settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]

I have also tried changing the way I load static files from:

{% load staticfiles %}

to:

{% load static %}

Still no luck. What is it I'm doing wrong?

6 Answers

Run into the same issue, after searching without any solution, I casually pressed Enter in CMD window, and python runserver continued and I found /static/css/blog.css was successfully found.

enter image description here

I had the some problem and the solution that works for me was stop the server using CRTL-BREAK and start again. I am using Django v.3.0.7.

If you are not extending from other html file as in your case then the first entry in your html file should be !DOCTYPE html and then the load template tag i.e {%load staticfiles%}.

For anyone who this after django removed the import of os in the settings file.

Use STATIC_ROOT = BASE_DIR / 'static' instead of os.path......

Related