2

I have looked around on this site as well as others and still have not found a solution that works for why django is not loading my css file

my settings.py file:

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

in my html file:

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

My file tree:

todo_app
    static
    todo_app
    todo_list
    db.sqlite3
    manage.py

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

Any help would be greatly appreciated. I tried using the STATIC_ROOT as shown on a different thread but there was no change with that.

4
  • Something says to me you should remove the first slash / in /css/my... Commented Nov 29, 2018 at 21:13
  • just tried that, still no change unfortunately Commented Nov 29, 2018 at 21:22
  • What happens when you check the source code in the browser, does it open the css link or says file not found? Commented Nov 30, 2018 at 3:40
  • It was saying the file was not found. Another member just helped me get it figured out though. I just needed to move the static directory within the app todo_list Commented Nov 30, 2018 at 16:14

7 Answers 7

6

Same Issue, Tried Crazy Solutions for 1 hour,

Found Problem : Browser cachs the css template and need to manually cleared to view the Updated CSS view.

Simple restarting the python manage.py runserver and

Opening the template in chrome's incognito mode solved the issue.

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

Comments

2

settings.py

# ....
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# ....

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

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

STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

urls.py

from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static


# ......


urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

8 Comments

I added the STATIC_ROOT = os.path.join(BASE_DIR, 'static_root') to the settings.py and then added the missing imports and urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to the urls.py. I put the new statement in the urls.py under the existing ones I already had in there. Is that correct or does it need to be within?
urlpatterns = [ ] add after
my django starter project link : github.com/ozkansen/Django-Starter-Project
Ok, I added it just like your example but there is still no change. When I do a view page source on the site after I run the server I am still seeing "css/my_style.css; could not be found"
I tried that, it compiled a bunch of files into a static_root folder. Still no luck. I am getting that same error
|
1

Hi I had the same problem and in my case, I solved with hard refresh.

  1. Stop the server with CTRL+C
  2. Update you CSS or load your CSS.
  3. Start the server with python manage.py runserver
  4. If you are using Chrome click CTRL + F5 for hard refresh.

Comments

0

I had this exact problem. Manually restarting the server fixed it. In the terminal running the server use:

Ctrl-c


python manage.py runserver


Comments

0

add following in settings.py

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

also, remove slash '/' from href, it should be"{% static 'css/my_style.css' %}"

Comments

0

I had also this problem.I again restart my server and it was fixed. I was stuck here. But after doing this it works perfectly.

> ctrl-c
> python3 manage.py runserver

Comments

-1

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

remove slash '/'

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.