Chace Mcguyer
Chace Mcguyer

Reputation: 415

Why is Django not loading my CSS?

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?

Upvotes: 3

Views: 10712

Answers (6)

Aditya Prasad Dhal
Aditya Prasad Dhal

Reputation: 180

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......

Upvotes: 0

user14177528
user14177528

Reputation: 1

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%}.

Upvotes: 0

m4u2o
m4u2o

Reputation: 43

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.

Upvotes: 0

NOZUONOHIGH
NOZUONOHIGH

Reputation: 2006

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

Upvotes: 0

user2584621
user2584621

Reputation: 2713

for django==2.0.2 none of the urls.py changes are necessary, just get STATIC_URL in settings.py right

Upvotes: 1

Diego Puente
Diego Puente

Reputation: 2004

I think you miss this in urls.py:

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

This work in dev, in production you must collectstatic with manage.py and serve statics with nginx(or apache).

Upvotes: 3

Related Questions