3

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 6

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yup that fixed my problem. Is this something new? I don't believe I had to do this in the last Django site I created.
This is not new, if you see version 1.7. It is there. docs.djangoproject.com/en/1.7/howto/static-files/…
That fixed my problem.
1

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

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.