0

my first question here. I'm still new to both Django and CSS.

I'm trying to use CSS from Static folder in my Django project. It's all at a pretty basic stage.

In the 'static' subfolder 'css' I have one main.css file with example code:

body{
    font-size: 20px;
    background-color: black;
}

h1{
    color: #008000;
}

In my settings.py file I have:

import os
(...)
DEBUG = True
(...)
STATIC_URL = '/static/'

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

And in my base.html template:

{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shopping List</title>
    <link rel="stylesheet" type="text/css" href="{% static '/css/main.css' %}">
</head>
<body>
<h1>TEST</h1>
{% block content %}
{% endblock %}
</body>
</html>

I've tried moving the css file, changing the view/url, and adding/deleting bootstrap (which alone works).

In the end my page just turns light blue-ish.

Thanks in advance.

sarek1910
  • 26
  • 2

2 Answers2

1

So I checked your code and for me it works fine check if you have 'django.contrib.staticfiles' in your INSTALLED_APPS

If this didn't work I'm going to leave my code here, hope it helps.

-DjangoProject
    -static
       -css
         -testing.css

template.html

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

settings.py

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

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

I just noticed that my STATICFILES_DIRS is a tuple and not a list like in your example, I don't think it makes any difference but you could try it.

andrew
  • 11
  • 1
  • Unfortunately that didn't help. I might try doing everything from scratch once again or using different environment. – sarek1910 Nov 23 '20 at 21:25
0

Not sure if this will work, but try removing the "/" before css in your html file:

... href="{% static 'css/main.css' %}">

Dennis
  • 55
  • 6