'Styles not loading in Django
I have a site that needs some custom styles in Django and I can't get the static file to load.
I have a static folder inside my main folder - The one where manage.py lives, Inside there is a CSS folder that contains a style.css.
At the top of base.html, I load
{% load static %}
Then in the head of my HTML I load
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
and in my settings.py file I have loaded in
# Static file route
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
When I load I just get a blank CSS file and no styles load, I'm fairly new to Django so please be kind, and thanks in advance.
Also, I want make sure my static folder is created in the right place:
Solution 1:[1]
Try
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
where BASE_DIR = Path(__file__).resolve().parent.parent
check also that you have
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
From your screen your static is css/style.css
but you load <link rel="stylesheet" href="{% static 'css/styles.css' %}">
instead.
You need to load <link rel="stylesheet" href="{% static 'css/style.css' %}">
Solution 2:[2]
You have to run in terminal:
python manage.py collectstatic
After every change of static files if I remember correctly.
Other then that, pressing F12 in a browser can show why a specific file isn't loading. So can the terminal/console where Django is running(or log files if no access to it).
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 | Gmuliu Gmuni |