I'm working on a pyhton django project and now I'm trying to add some style to it with a styles.css file but I just can't get it to work.
My project is named commerce and my app is called auctions and I have the static file under commerce/auctions/static/auctions/styles.css
My settings.py file include
INSTALLED_APPS = [
'auctions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
My auctions/urls.py
from django.urls import path
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
if settings.DEBUG:
urlpatterns = staticfiles_urlpatterns()
My styles.css file
h1 {
color: red;
}
Then in my template file, I have this
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Auctions{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'auctions/styles.css' %}">
</head>
<body>
<h1>Auctions</h1>
</body>
</html>
It feels like I'm missing something obvious.
EDIT: So I managed to fix my issue, and it was the STATIC_URL in my settings.py that was wrong, it looked in the wrong place. I updated it to:
STATIC_URL = 'auctions/static/'
And now it works. Thanks for your replies and help.
CodePudding user response:
you need to add this in your settings.py :
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
this should resolve the issue.
CodePudding user response:
Edit your settings.py file and add WhiteNoise (check here the lib) to the MIDDLEWARE list, above all other middleware apart from Django’s SecurityMiddleware:
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
You should also add this to the settings.py:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
For more details,check Using WhiteNoise with Django guide.